连接到Google Cloud Datastore时出错

时间:2014-02-01 11:22:40

标签: c# google-cloud-datastore

我尝试使用.NET驱动程序beta1在Google Cloud DataStore中插入实体。这是代码:

String serviceAccountEmail = "***.apps.googleusercontent.com";

var certificate = new X509Certificate2(
    @"***-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
   new ServiceAccountCredential.Initializer(serviceAccountEmail) {
       Scopes = new[] {
           Google.Apis.Datastore.v1beta1.DatastoreService.Scope.Datastore.ToString().ToLower()
       }
   }.FromCertificate(certificate));

// Create the service.
var service = new DatastoreService(new BaseClientService.Initializer() {
    HttpClientInitializer = credential,
    ApplicationName = "***",
});

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 = "KindTest", Name = "name" });
var firstName = new GoogleData.Property();
firstName.Values = new List<GoogleData.Value>();
firstName.Values.Add(new GoogleData.Value { StringValue = "FName" });
entity.Properties = new Dictionary<string, Property>();

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, "***").Execute();

错误是:

"invalid_grant", Description:"", Uri:""

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我相信您还需要userinfo.email范围:

https://www.googleapis.com/auth/userinfo.email

此外 - 确保您已启用Cloud Datastore API(请参阅this answer)。