gcloud auth activate-service-account --key-file $JSON_KEY_FILE
gcloud ai endpoints raw-predict $ENDPOINT_ID \
--project=$PROJECT_ID \
--region=$LOCATION_ID \
--http-headers=Content-Type=application/json \
--request=$JSON_BODY
gcloud auth activate-service-account --key-file $JSON_KEY_FILE
curl -X POST "https://$LOCATION_ID-aiplatform.googleapis.com/v1beta1/projects/$PROJECT_ID/locations/$LOCATION_ID/endpoints/$ENDPOINT_ID:rawPredict" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
--data $JSON_BODY
using Xunit;
using Google.Apis.Auth.OAuth2;
using Google.Protobuf;
using Google.Api;
using System.Threading.Tasks;
using Google.Cloud.AIPlatform.V1Beta1; // also tried just V1
namespace UnitTests;
public class UnitTest1
{
[Fact]
public async Task Test1()
{
var jsonKeyFile = "<path to json key file>";
var credential = GoogleCredential.FromFile(jsonKeyFile);
var client = await new PredictionServiceClientBuilder
{
Credential = credential,
}.BuildAsync();
var response = await client.RawPredictAsync( // error: 404
"projects/<project-id>/locations/<location-id>/endpoints/<endpoint-id>",
new HttpBody
{
Data = ByteString.CopyFromUtf8(@"<json body>")
});
}
}
Heard back from some internal Google contacts that the`Google.Cloud.AIPlatform.V1` and `Google.Cloud.AIPlatform.V1beta1` C# NuGet packages have not been updated to use the new Location-prefixed version of the `aiplatform.googleapis.com` domain. Therefore, I had to workaround the above by getting an accessToken and constructing the Authorization header myself like done in the `curl` example. The code to get an accessToken:
var credential = GoogleCredential
.FromFile(jsonKeyFile)
.CreateScoped("https://www.googleapis.com/auth/cloud-platform");
var accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();