【问题标题】:403 permission denied on Google Cloud Datastore .NET client APIGoogle Cloud Datastore .NET 客户端 API 上的 403 权限被拒绝
【发布时间】: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 控制台,它可以工作。

** 更新 **

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

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

【问题讨论】:

  • 我认为这是同样的问题?你能把你的代币和我的比较一下吗? (stackoverflow.com/questions/17094641/…)
  • 能否详细说明您是如何创建服务帐号的?
  • @proppy 见上面的更新

标签: c# .net google-api oauth-2.0 google-cloud-datastore


【解决方案1】:

question 相同的答案。

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

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

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

【讨论】:

  • 这并没有解决问题,因为创建服务帐户实际上会将该电子邮件地址自动添加到团队成员。但是,我在另一篇文章中找到了您和作者之间的聊天 [chat.stackoverflow.com/rooms/32113/…,并且禁用和重新启用 Cloud Datastore API 也为我解决了这个问题。
猜你喜欢
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 2022-08-03
  • 1970-01-01
  • 2020-08-05
  • 2017-04-13
相关资源
最近更新 更多