【问题标题】:Google+ Moments.insert - Unauthorized ErrorGoogle+ Moments.insert - 未经授权的错误
【发布时间】:2014-12-14 19:03:19
【问题描述】:

根据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 您的应用程序将编写的活动。

我还没有弄清楚如何设置这个 data-requestvisibleactions。

【问题讨论】:

  • 这很奇怪。我同意。尝试添加"profile" scopedocumentation 中提到 UserInfoEmail 和 UserInfoProfile 都已弃用。
  • @peleyal 配置文件没有作为 PlusService.Scope 中的范围出现,客户端库中可能缺少它吗?你知道什么是 data-requestvisibleactions 吗?
  • 看起来它要与 java 一起工作你必须做一些 hackie 将它添加到 url developers.google.com/+/web/app-activities
  • 您可以添加“profile”(字符串)并检查一下吗?它最终对你有用吗?我的意思是使用您刚刚分享的链接...
  • 不,我还没有让它工作。 “配置文件”范围对同样的错误没有帮助。

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


【解决方案1】:

您已经注意到,您需要将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 属性,这将为您处理这部分身份验证。

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    相关资源
    最近更新 更多