0

Hello I'm trying to use the google calendar API. I'm unable to obtain an access token.

Here is the code I'm currently using:

  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();
    }

The api documentation I'm following is here: https://developers.google.com/accounts/docs/OAuth2InstalledApp

I'm constantly getting HTTP Bad Request 400 error.

I have tried the solution given in this question but it doesn't work either: Google Calendar API - Bad Request (400) Trying To Swap Code For Access Token

Can someone please point out what I'm doing wrong here?

4

1 回答 1

0

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

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

于 2012-05-15T02:41:31.060 回答