Google+ Moments.insert - 未经授权的错误

时间:2014-12-14 19:03:19

标签: c# google-plus google-api-dotnet-client

根据Moments.insert与Google+ API的文档说明,需要使用以下范围进行身份验证

https://www.googleapis.com/auth/plus.login

我正在使用所有可能的PlusService范围进行身份验证,但我仍然收到以下错误

  

Google.Apis.Requests.RequestError未经授权[401]错误[     消息[未经授权]位置[ - ]原因[未经授权]   域[全球]

 //Scopes for use with Google+ API
 // activating Google+ API in console
 // Documentation:  https://developers.google.com/+/api/oauth
 string[] scopes = new string[] {
    PlusService.Scope.PlusLogin,
    PlusService.Scope.UserinfoEmail,
    PlusService.Scope.UserinfoProfile
 };

 string _client_id = "2046123799103-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
 string _client_secret = "NDmluNfTgUk6wgmy7cFo64RV";

 PlusService service = null;
 UserCredential credential = null;

 try {
    // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
        ClientId = _client_id, ClientSecret = _client_secret
    },
    scopes,
    Environment.UserName,
    CancellationToken.None,
    new FileDataStore("Daimto.GooglePlus.Auth.Store")).Result;
 } catch (Exception ex) {
    //If the user hits cancel you wont get access.
    if (ex.InnerException.Message.IndexOf("access_denied") != -1) {
        Console.WriteLine("User declined access");
        Console.ReadLine();
        return;
    } else {
        Console.WriteLine("Unknown Authentication Error:" + ex.Message);
        Console.ReadLine();
        return;
    }
 }

 // Now we create a Google service. All of our requests will be run though this.
 service = new PlusService(new BaseClientService.Initializer() {
    HttpClientInitializer = credential,
    ApplicationName = "Google Plus Sample",
 });


 Moment body = new Moment();
 body.Type = "http://schema.org/AddAction";

 ItemScope itemScope = new ItemScope();
 itemScope.Id = "target-id-1";
 itemScope.Type = "http://schema.org/AddAction";
 itemScope.Name = "The Google+ Platform";
 itemScope.Description = "A page that describes just how awesome Google+ is!";
 itemScope.Image = "https://developers.google.com/+/plugins/snippet/examples/thing.png";
 body.Object = itemScope;

 try {
    var l = service.Moments.Insert(body, "me", MomentsResource.InsertRequest.CollectionEnum.Vault);
    l.Execute();
 } catch (Exception ex) {
    int i = 1;
 }

我已经测试了身份验证,它正在运行,我可以列出活动和其他内容。它唯一的插入时刻给了我这个错误。我也试过在PHP中这样做,我得到同样的错误。我错过了什么?

更新:我在moments.insert

的文档中找到了一些内容
  

在对moment.insert进行身份验证时,您必须包含   data-requestvisibleactions参数指定哪些类型的App   您的申请将要写的活动。

我还没想出如何设置这个数据请求可见的行为。

1 个答案:

答案 0 :(得分:0)

正如您所注意到的,您需要将request_visible_actions参数添加到请求中。来自Google的大多数其他OAuth库都添加了一个快捷方式来执行此操作,但看起来.NET库还没有。例如,PHP库有setRequestVisibleActions()

在内部,便捷方法GoogleWebAuthorizationBroker.AuthorizeAsync()调用AuthorizationCodeFlow.CreateAuthorizationCodeRequest()来生成调用中使用的URL。您可以继承AuthorizationCodeFlowAuthorizationCodeRequestUrl(它返回)以添加有问题的参数,然后更直接地浏览流程。

您可以在https://github.com/gguuss/google-dotnet-demo/blob/master/AuthWithAppActivities/Program.cs

查看如何执行此操作的示例

您还可以使用Google+ Sign-In button执行初始身份验证流程并将一次性代码传递给服务器,然后服务器可以继续将其转换为有效令牌。由于登录按钮具有data-requestvisibleactions属性,因此将为您处理身份验证的这一部分。