如何使用C#和Curl的API键调用Google顶点AI端点

发布于 2025-02-10 07:47:29 字数 475 浏览 4 评论 0 原文

从AC#程序中,我想称呼我的顶点AI端点 api键 (通过“ Google cloud”/recertentials/api键“

>

​: curl -x帖子 -h“ apikey = .. mykey ...” -h“ content-type:application/json” https://...googleapis.com/v1/projects/ [Project_ID]端点/[endpoint_id]:预测 -d @image.json

错误: “错误”: { “代码”:401, “消息”:“缺少请求,需要身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。 }

我的问题: 有没有一种方法可以使用Apikey进行身份验证来验证顶点AI?

from a c# program i want to call my vertex ai endpoint for prediction with an api key (via "Google Cloud"/Credentials/API Keys" )). I gave the api key access to Vertex AI and as a test everything else too.

calling it with curl or c# i get the error that the service expects an "OAuth 2 access token " or something else.

CURL:
curl -X POST
-H "apikey=..mykey..." -H "Content-Type: application/json" https://.....googleapis.com/v1/projects/[PROJECT_ID]/locations/europe-west4/endpoints/[ENDPOINT_ID]:predict
-d @image.json

ERROR:
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. ...
}

MY QUESTION:
Is there a way to use the Apikey for authentication to vertex ai?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装爱人 2025-02-17 07:47:29

您无法将API密钥用于凭据。您必须通过Google身份验证或Google Service帐户获得承载令牌,

此错误将显示您是否在此处使用API​​密钥

Request is missing required authentication credential.
 
Expected OAuth 2 access token, login cookie or other valid authentication credential. 

See https://developers.google.com/identity/sign-in/web/devconsole-project.

,我将显示如何生成携带者令牌: https://stackoverflow.com/a/75963509/11096674

You could not use the API key for credentials. You must get the Bearer token via Google authentication or Google Service account

This error will show if you are using API Key

Request is missing required authentication credential.
 
Expected OAuth 2 access token, login cookie or other valid authentication credential. 

See https://developers.google.com/identity/sign-in/web/devconsole-project.

Here I will show how to generate Bearer Token: https://stackoverflow.com/a/75963509/11096674

迷迭香的记忆 2025-02-17 07:47:29

我看到这有点老了,但是我遇到了同样的问题,一周后能够解决它。

步骤

以下是使用此代码在我中使用此代码的

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        // Define the endpoint URL
        var endpointUrl = $"https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/endpoints/{endpoint_id}:predict";

        // Read the image file as bytes
        var imageData = File.ReadAllBytes(filename);

        // Convert the image data to base64
        var imageBase64 = Convert.ToBase64String(imageData);

        // Define the request body
        var requestBody = new
        {
            instances = new[]
            {
                new {
                    content = imageBase64
                }
            }
        };

        // Serialize the request body to JSON
        var requestBodyJson = JsonConvert.SerializeObject(requestBody);

        // Create an HTTP client and request message
        using var client = new HttpClient();
        using var request = new HttpRequestMessage(HttpMethod.Post, endpointUrl);
        request.Content = new StringContent(requestBodyJson, System.Text.Encoding.UTF8, "application/json");

        // Set the authentication header
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_ACCESS_TOKEN");

        // Send the request and get the response
        using var response = await client.SendAsync(request);
        var responseBodyJson = await response.Content.ReadAsStringAsync();

        // Deserialize the response body from JSON
        dynamic responseBody = JsonConvert.DeserializeObject(responseBodyJson);

        // Process the response
        // ...
    }
}

,邮件问题是your_access_token,这不是API密钥。

o获取访问令牌以调用顶点AI端点,您需要使用具有必要权限来调用端点的Google Cloud凭据进行身份验证。

以下是您可以使用Google Cloud SDK获取访问令牌的方式:

通过在此处按照说明安装Google Cloud SDK: https://cloud.google.com/sdk/install

打开终端或命令提示符,然后运行以下命令使用您的Google Cloud凭据进行身份验证:

gcloud auth application-default login

请按照提示符登录到您的Google Cloud帐户,并将权限授予Google Cloud SDK。

经过身份验证后,您可以通过运行以下命令获得访问令牌:

gcloud auth application-default print-access-token

这将输出一个访问令牌,您可以使用该命令来验证您的请求到顶点AI端点。

在我之前提供的示例代码片段中,将您使用上述步骤获得的访问令牌替换为your_access_token。

应该解决它

I see this is a bit old but I was having the same issue and after a week was able to solve it.

Here are the steps

Using this code in c#

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        // Define the endpoint URL
        var endpointUrl = 
quot;https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/endpoints/{endpoint_id}:predict";

        // Read the image file as bytes
        var imageData = File.ReadAllBytes(filename);

        // Convert the image data to base64
        var imageBase64 = Convert.ToBase64String(imageData);

        // Define the request body
        var requestBody = new
        {
            instances = new[]
            {
                new {
                    content = imageBase64
                }
            }
        };

        // Serialize the request body to JSON
        var requestBodyJson = JsonConvert.SerializeObject(requestBody);

        // Create an HTTP client and request message
        using var client = new HttpClient();
        using var request = new HttpRequestMessage(HttpMethod.Post, endpointUrl);
        request.Content = new StringContent(requestBodyJson, System.Text.Encoding.UTF8, "application/json");

        // Set the authentication header
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_ACCESS_TOKEN");

        // Send the request and get the response
        using var response = await client.SendAsync(request);
        var responseBodyJson = await response.Content.ReadAsStringAsync();

        // Deserialize the response body from JSON
        dynamic responseBody = JsonConvert.DeserializeObject(responseBodyJson);

        // Process the response
        // ...
    }
}

for me the mail issue was the YOUR_ACCESS_TOKEN which is not the API key.

o obtain an access token for calling a Vertex AI endpoint, you need to authenticate using Google Cloud credentials that have the necessary permissions to call the endpoint.

Here's how you can obtain an access token using the Google Cloud SDK:

Install the Google Cloud SDK by following the instructions here: https://cloud.google.com/sdk/install.

Open a terminal or command prompt and run the following command to authenticate using your Google Cloud credentials:

gcloud auth application-default login

Follow the prompts to log in to your Google Cloud account and grant permissions to the Google Cloud SDK.

Once you're authenticated, you can obtain an access token by running the following command:

gcloud auth application-default print-access-token

This will output an access token that you can use to authenticate your requests to the Vertex AI endpoint.

In the example code snippets I provided earlier, replace YOUR_ACCESS_TOKEN with the access token you obtained using the above steps.

That should solve it

瞄了个咪的 2025-02-17 07:47:29

这是一个封闭的测试版,但是如果您有访问权限,现在可以使用Google MakerSuite生成一个简单的API密钥。参见在MakerSuite中为Vertexai生成API密钥,但目前处于封闭的beta中。然后只需使用& key = thatkey调用顶点AI API

It is a closed Beta, but if you have access, now you can use google makersuite to generate a simple api key. See step-by-step to generate API key for vertexAI in makersuite, but currently is on a closed beta. then just call the vertex AI api with &key=thatKey

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文