繁体   English   中英

Google Cloud Datastore .NET客户端API拒绝403权限

[英]403 permission denied on Google Cloud Datastore .NET client API

使用新的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控制台,则可以使用。

**更新**

这是我创建服务帐户的方式:

  1. 已创建AppEngine应用程序。
  2. 导航到Google API控制台。
  3. 为AppEngine应用程序启用了Google Cloud Datastore API。
  4. 点击“创建OAuth 2.0客户端ID ...”
  5. 给它起一个假名。
  6. 选择“服务帐户”作为应用程序类型。
  7. 单击“创建客户端ID”。
  8. 单击“下载私钥”(在下面的代码中表示为TestClientCredentials.ClientCertificateFilePath的位置)。

与此问题的答案相同。

为了正确地为您的Cloud Datastore实例配置服务帐户,您必须按照文档中所述使用Cloud Console创建它们。

或者,如果您确实要使用通过[Google API控制台] [3]创建的服务帐户,则可以执行以下操作:

  • 转到cloud.google.com/console
  • 点击您的项目ID
  • 点击⚙
  • 点击团队
  • 点击添加会员
  • 将您的服务帐户添加为查看者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM