【问题标题】:Inserting moment to google plus with google-api-dotnet-client WP8 error 401使用 google-api-dotnet-client WP8 错误 401 向 google plus 插入时刻
【发布时间】:2014-03-13 10:11:58
【问题描述】:

我现在正在尝试从 Windows Phone 应用程序添加片刻两天,但无法正常工作。没有真正的教程或做过什么...无论如何,从我到目前为止可以登录的各种来源,请求范围(即使由于某种原因没有请求 login.plus 范围),我尝试发布片刻。这是我能够创建的代码

List<string> scopes = new List<string>();
        scopes.Add("https://www.googleapis.com/auth/plus.login");    
        scopes.Add("https://www.googleapis.com/auth/userinfo.email");

        var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "<My client ID>",
                ClientSecret = "<My Client Secret>"
            },
            scopes,
            "user",
            CancellationToken.None);

        var initializer = new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "WP Drive Sample Application",
        };

        var plusService = new PlusService(initializer);

        Moment body = new Moment();
        ItemScope target = new ItemScope();
        target.Id = "<Unique ID>";
        target.Image = "http://www.google.com/s2/static/images/GoogleyEyes.png";
        target.Type = "";
        target.Description = "The description for the activity";
        target.Name = "An example of add activity";

        body.Target = target;
        body.Type = "http://schemas.google.com/AddActivity";

        MomentsResource.InsertRequest insert =
            new MomentsResource.InsertRequest(
                plusService,
                body,
                "me",
                MomentsResource.InsertRequest.CollectionEnum.Vault);
        Moment wrote = await insert.ExecuteAsync();

当我尝试使用 insert.ExecuteAsync(); 时出现此错误

    {The service plus has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Unauthorized [401]
Errors [
    Message[Unauthorized] Location[ - ] Reason[unauthorized] Domain[global]
]

   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Google.Apis.Requests.ClientServiceRequest`1.<ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at GooglePlus.MainPage.<Test>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state)}

经过一番搜索,我发现我无法访问它的问题可能是我没有使用 request_visible_actions。但问题是我不知道如何用我的代码请求它,而且我在互联网上找不到任何可以在我的 windows phone 应用程序中使用的 c sharp 资源。知道如何解决此问题并最终能够从我的应用程序中发布片刻吗?

【问题讨论】:

  • 您是否查看了该链接:ikaisays.com/2013/07/19/…。在我看来,您没有启用 API。
  • 我启用了 Google+ API
  • 你搞定了吗?
  • 没有解决。研究了一段时间,但找不到解决方案,所以我放弃了它。当我有时间时,我会尝试再做一次。

标签: c# windows-phone-8 google-plus google-api-dotnet-client


【解决方案1】:

几点说明:

  1. 您的电子邮件范围不正确,最近已更新为“电子邮件”
  2. 发生 401 是因为您缺少用于编写应用活动的 request_visible_actions。

对于 2,您可以使用 Google+ 登录按钮执行授权请求,也可以更改请求 URI 以推送附加参数。下面展示了如何创建设置了 request_visible_actions 的登录按钮:

<span id="signinButton">
  <span
    class="g-signin"
    data-callback="signinCallback"
    data-clientid="CLIENT_ID"
    data-accesstype="offline"
    data-cookiepolicy="single_host_origin"
    data-requestvisibleactions="http://schemas.google.com/AddActivity"
    data-scope="https://www.googleapis.com/auth/plus.login">
  </span>
</span>

登录按钮的回调返回一个授权代码,您可以用该代码交换访问/刷新令牌,然后可以用于进行 API 调用以将应用活动写入 Google。因为您使用的是 Windows Phone,所以我不太确定这应该如何工作。

无论如何,缺少的 KEY 部分很可能是 requestvisibleactions 值。如果您缺少 request_visible_actions,您的同意对话框将如下所示:

要验证它请求的权限是否正确,您将看到一个包含以下内容的同意对话框:

如果系统提示用户进行应用活动,则说明您正确设置了范围,并且应该具有权限。

如果您请求正确的范围,但您的项目缺少适当的 API(Google developer console 中的 Google+ API),您将继续看到 401 错误 - 请确保您已添加 Google+。

我现在创建了一个控制台项目来演示how to request the Google+ app activity types this from native apps - 检查

存在一个已知错误,即在您授权没有应用活动权限的用户后,您必须disconnect and reconnect the app 才能获得该权限。

【讨论】:

  • 正如我在问题中所说,我已经认为我的问题是我需要那个 request_visible_actions 参数,并且我已经说过提示权限时范围没有出现。我已经考虑过使用登录按钮,但这对我来说并不是一个真正的选择,因为我希望有类似的感觉,就像我的其他分享选项一样(另外我什至不知道我是否可以使用它)。我的问题是,要呈现 HTML,我只能使用 WebBrowser 组件。因此,要呈现签名按钮,我需要创建一个 html,以某种方式监听请求更改等......这似乎有点矫枉过正。
  • 已确认,仍在进一步调查。
  • @class 你是否将“response_type”参数设置为“code”?
  • @Supertecnoboff 感谢您的推动。我会尽快看看这个。按钮需要 data-accesstype="offline"
  • @G.T.您是否设法解决了这个问题,我也遇到了同样的问题。在互联网上搜索了所有内容。能否请您发布您的解决方案。
猜你喜欢
  • 1970-01-01
  • 2013-05-02
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多