문제

I would like to implement Google’s Oauth2 in an in-house application and was playing with the demo found here using a C# console app:

https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs

I have added all the necessary Google API libraries to my project(Core client,YouTube,Contacts……) and registered my application…..

When I execute the program I am able to get back an access code from the authorized client etc…… I am using the correct clientID, ClientSecret key…….

I eventually end up getting an exception: The remote server returned an error: (400) Bad Request.

Thrown from this method:

    public static void GetAccessToken(OAuth2Parameters parameters) {
      OAuthBase.GetOAuth2AccessToken(parameters, OAuthBase.GetExchangeAccessCodeRequestBody(parameters));
    }

Which calls:

public static void GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody) { Uri requestUri = new Uri(parameters.TokenUri); WebRequest request = WebRequest.Create(requestUri); request.Method = "POST";

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

        Stream outputStream = request.GetRequestStream();
        StreamWriter w = new StreamWriter(outputStream);
        w.Write(requestBody);
        w.Flush();
        w.Close();

        WebResponse response = request.GetResponse();
        string result = "";
        if (response != null) {
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            result = reader.ReadToEnd();

            Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);

            if (dict.ContainsKey(OAuth2AccessToken)) {
              parameters.AccessToken = dict[OAuth2AccessToken];
            }
            if (dict.ContainsKey(OAuth2RefreshToken)) {
              parameters.RefreshToken = dict[OAuth2RefreshToken];
            }
            if (dict.ContainsKey(OAuth2TokenType)) {
              parameters.TokenType = dict[OAuth2TokenType];
            }
            if (dict.ContainsKey(OAuth2ExpiresIn)) {
              parameters.TokenExpiry = DateTime.Now.AddSeconds(int.Parse(dict[OAuth2ExpiresIn]));
            }
        }
    }

Can someone please explain what may be causing the below error?

System.Net.WebException was unhandled
  HResult=-2146233079
  Message=The remote server returned an error: (400) Bad Request.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at Google.GData.Client.OAuthBase.GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody) in c \Development\lib\YouTube\libgoogle-data-mono-2.1.0.0\src\core\oauthbase.cs:line 435
       at Google.GData.Client.OAuthUtil.GetAccessToken(OAuth2Parameters parameters) in c:\ \lib\YouTube\libgoogle-data-mono-2.1.0.0\src\core\oauthutil.cs:line 246
       at GoogleAppsConsoleApplication.OAuth2Demo.Main(String[] args) in c:\Users\test\Documents\Visual Studio 2012\Projects\GoogleAppsConsoleApplication\GoogleAppsConsoleApplication\Program.cs:line 60
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
도움이 되었습니까?

해결책

You are working with the old gdata protocol. I recommend you to work with Google APIs client library. Take a look at https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 for details on how to work with OAuth2.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top