Google ML 引擎从 C# 身份验证问题预测
Google ML-Engine Predict from C# Authentication issue
已关注
OAuth example
成功获取不记名令牌,但响应是:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Vary: X-Origin
Vary: Referer
Vary: Origin
Vary: Accept-Encoding
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35"
Transfer-Encoding: chunked
Accept-Ranges: none
Cache-Control: private
Date: Thu, 03 May 2018 13:29:53 GMT
Server: ESF
WWW-Authenticate: Bearer realm="https://accounts.google.com/"
Content-Type: application/json; charset=UTF-8
}}
使用具有 'ML Engine Developer' 角色的服务帐户。
这是代码:
var url = $"{googleapiprojecturl}/models/{modelname}/versions/{version}:predict";
GoogleCredential credential;
using (Stream stream = new FileStream(@"C:\serviceacctkey.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream);
}
var bearer_token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync(url);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer_token);
var content = new StringContent(payloadJsonString, Encoding.UTF8, "application/json");
var responseMessage = await client.PostAsync(url, content);
responseMessage.EnsureSuccessStatusCode();
其中 googleapiprojecturl = https://ml.googleapis.com/v1/projects/{projectID}
我没有在 C# 中完成此操作,但我在 Python 中也遇到了以下类似代码的问题:
# Doesn't work
# creds = GoogleCredentials.from_stream(SERVICE_ACCOUNT_FILE)
在 Python 中,下面的代码起作用了:
from oauth2client import service_account
creds = service_account.ServiceAccountCredentials.from_json_keyfile_name('key.json', SCOPES)
creds.get_access_token()
在 C# 中,您似乎会使用 ServiceAccountCredentials
class。
正如克里斯在上面所建议的那样,作为对问题的评论,答案是在请求令牌之前的凭证范围:
credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { CloudMachineLearningEngineService.Scope.CloudPlatform });
已关注
OAuth example
成功获取不记名令牌,但响应是:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Vary: X-Origin
Vary: Referer
Vary: Origin
Vary: Accept-Encoding
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35"
Transfer-Encoding: chunked
Accept-Ranges: none
Cache-Control: private
Date: Thu, 03 May 2018 13:29:53 GMT
Server: ESF
WWW-Authenticate: Bearer realm="https://accounts.google.com/"
Content-Type: application/json; charset=UTF-8
}}
使用具有 'ML Engine Developer' 角色的服务帐户。 这是代码:
var url = $"{googleapiprojecturl}/models/{modelname}/versions/{version}:predict";
GoogleCredential credential;
using (Stream stream = new FileStream(@"C:\serviceacctkey.json", FileMode.Open, FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(stream);
}
var bearer_token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync(url);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer_token);
var content = new StringContent(payloadJsonString, Encoding.UTF8, "application/json");
var responseMessage = await client.PostAsync(url, content);
responseMessage.EnsureSuccessStatusCode();
其中 googleapiprojecturl = https://ml.googleapis.com/v1/projects/{projectID}
我没有在 C# 中完成此操作,但我在 Python 中也遇到了以下类似代码的问题:
# Doesn't work
# creds = GoogleCredentials.from_stream(SERVICE_ACCOUNT_FILE)
在 Python 中,下面的代码起作用了:
from oauth2client import service_account
creds = service_account.ServiceAccountCredentials.from_json_keyfile_name('key.json', SCOPES)
creds.get_access_token()
在 C# 中,您似乎会使用 ServiceAccountCredentials
class。
正如克里斯在上面所建议的那样,作为对问题的评论,答案是在请求令牌之前的凭证范围:
credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { CloudMachineLearningEngineService.Scope.CloudPlatform });