【发布时间】:2013-06-10 06:28:51
【问题描述】:
使用新的 Google Cloud Datastore v1beta 客户端库,我得到了
{
"error": {
"errors": [
{
"domain": "global",
"reason": "PERMISSION_DENIED",
"message": "Unauthorized."
}
],
"code": 403,
"message": "Unauthorized."
}
}
任何请求。我创建了一个应用引擎应用,添加了 Cloud Datastore API,配置了一个服务帐号,并使用它来验证我的请求。
[TestMethod]
public void BasicBlindWrite()
{
var service = new DatastoreService(new BaseClientService.Initializer() { Authenticator = CreateAuthenticator() });
var request = new GoogleData.BlindWriteRequest();
var entity = new GoogleData.Entity();
entity.Key = new GoogleData.Key();
entity.Key.Path = new List<KeyPathElement>();
entity.Key.Path.Add(new GoogleData.KeyPathElement { Kind = "Consumer", Name = "Consumer-1" });
var firstName = new GoogleData.Property();
firstName.Values = new List<GoogleData.Value>();
firstName.Values.Add(new GoogleData.Value { StringValue = "Samuel"});
entity.Properties = new GoogleData.Entity.PropertiesData();
entity.Properties.Add("FirstName", firstName);
request.Mutation = new GoogleData.Mutation();
request.Mutation.Upsert = new List<GoogleData.Entity>();
request.Mutation.Upsert.Add(entity);
var response = service.Datasets.BlindWrite(request, "my-appengine-project-id").Fetch();
}
private OAuth2Authenticator<AssertionFlowClient> CreateAuthenticator()
{
var certificate = new X509Certificate2(TestClientCredentials.ClientCertificateFilePath, "notasecret",
X509KeyStorageFlags.Exportable);
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = TestClientCredentials.CertificateEmailAddress,
Scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/datastore"
};
var authenticator = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
return authenticator;
}
如果我使用 Web API 控制台,它可以工作。
** 更新 **
以下是我创建服务帐户的方式:
- 已创建 AppEngine 应用程序。
- 导航到 Google API 控制台。
- 为 AppEngine 应用启用了 Google Cloud Datastore API。
- 单击“创建 OAuth 2.0 客户端 ID...”
- 给它起一个假名。
- 选择“服务帐户”作为应用程序类型。
- 单击“创建客户端 ID”。
- 单击“下载私钥”(在下面的代码中,位置表示为 TestClientCredentials.ClientCertificateFilePath)。
【问题讨论】:
-
我认为这是同样的问题?你能把你的代币和我的比较一下吗? (stackoverflow.com/questions/17094641/…)
-
能否详细说明您是如何创建服务帐号的?
-
@proppy 见上面的更新
标签: c# .net google-api oauth-2.0 google-cloud-datastore