【问题标题】:C# : Unable to obtain Accesstoken. Google calendar APIC#:无法获取访问令牌。谷歌日历 API
【发布时间】:2012-05-15 01:52:37
【问题描述】:

您好,我正在尝试使用谷歌日历 API。我无法获得访问令牌。

这是我目前使用的代码:

  private string ObtainAccessToken(string code)
    {
        string content =
                         "code=" + code + "&" +
                        "client_id=1016429729591.apps.googleusercontent.com&"+ 
                        "client_secret={MySecret}&" +
                        "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" +
                        "grant_type=authorization_code";//authorization_code";
        Google.Apis.Authentication.HttpRequestFactory
                factory = new Google.Apis.Authentication.HttpRequestFactory();


        HttpWebRequest req = factory.Create(new Uri("https://accounts.google.com/o/oauth2/token?"+content), "POST");
        byte[] data = Google.Apis.Utilities.EncodeStringToUtf8(content);
        req.ContentLength = data.Length;

        req.ContentType = "application/x-www-form-urlencoded";

   req.GetRequestStream().Write(data, 0, data.Length);
        req.GetResponse();
    }

我关注的 api 文档在这里:https://developers.google.com/accounts/docs/OAuth2InstalledApp

我经常收到 HTTP Bad Request 400 错误。

我已经尝试过这个问题中给出的解决方案,但它也不起作用: Google Calendar API - Bad Request (400) Trying To Swap Code For Access Token

有人可以指出我在这里做错了什么吗?

【问题讨论】:

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


    【解决方案1】:

    查看此示例:http://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.SimpleOAuth2/Program.cs?repo=samples

    这是一个命令行应用,使用 OAuth 2.0 来授权 Tasks API 请求,它应该很容易改用日历 API。

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 2017-11-13
      • 2016-05-20
      • 2014-05-04
      • 2021-05-06
      • 2014-10-17
      • 2013-07-20
      • 2016-04-19
      • 2020-01-05
      相关资源
      最近更新 更多