How to get access token for google oauth?

前端 未结 5 1785
离开以前
离开以前 2021-02-05 21:30

I am using C# (ASP.NET). I want to use Google OAuth for accessing the user profile detail in my app. I successfully got the authorization code but having a problem in getting th

相关标签:
5条回答
  • 2021-02-05 21:57

    As I had similar problems in the process of implementing Google auth, I will post the code that works.. The last mentioned problem: error (400) Bad request could be caused by leading '?' in the above code..

     string codeClient = "code="+ t +"&client_id=number.apps.googleusercontent.com&";
     string secretUri = "client_secret=yoursecret&" + "redirect_uri=path&"
          + "grant_type=authorization_code";
     postString = codeClient + secretUri;
    
     string url = "https://accounts.google.com/o/oauth2/token";
    
     HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url.ToString());
     request.Method = "POST";
     request.ContentType = "application/x-www-form-urlencoded";
    
     UTF8Encoding utfenc = new UTF8Encoding();
     byte[] bytes = utfenc.GetBytes(postString);
     Stream os = null;
     try
     {
          request.ContentLength = bytes.Length;
          os = request.GetRequestStream();
          os.Write(bytes, 0, bytes.Length);
     }
     catch
     { }
    
     try
     {
          HttpWebResponse webResponse = (HttpWebResponse) request.GetResponse();
          Stream responseStream = webResponse.GetResponseStream();
          StreamReader responseStreamReader = new StreamReader(responseStream);
          result = responseStreamReader.ReadToEnd();//parse token from result
    
    0 讨论(0)
  • 2021-02-05 21:57

    My code is working, I have done mistakes in above two lines. It should be like this

    byte[] buffer = Encoding.ASCII.GetBytes("code=" + code + "&client_id=xxx&client_secret=xxx&redirect_uri=xxxx&grant_type=authorization_code");
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
    

    Remaining code is correct.

    0 讨论(0)
  • 2021-02-05 22:06

    I think you are sending the POST request to the wrong endpoint, the correct one is https://accounts.google.com/o/oauth2/token

    0 讨论(0)
  • 2021-02-05 22:12

    The original request seems to be somewhat outdated. But I found that the Google's code examples contain lots of "Best Practices" housekeeping code that's hard to separate from the essential operations.

    I recently published a document that represents all the REST operations as curl commands. It's hard to be conversant in every language, but curl seems universal. Most people know it- otherwise, it's pretty easy to grasp. In my curl examples, the -d flag indicates a POST operation. Otherwise, the parameters are appended to the URL.

    http://www.tqis.com/eloquency/googlecalendar.htm

    0 讨论(0)
  • 2021-02-05 22:21
    public string ReceiveTokenGmail(string code, string GoogleWebAppClientID, string GoogleWebAppClientSecret, string RedirectUrl)
    {
        string postString = "code=" + code + "&client_id=" + GoogleWebAppClientID + @"&client_secret=" + GoogleWebAppClientSecret  + "&redirect_uri=" + RedirectUrl;
    
        string url = "https://accounts.google.com/o/oauth2/token";
    
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
    
        UTF8Encoding utfenc = new UTF8Encoding();
        byte[] bytes = utfenc.GetBytes(postString);
        Stream os = null;
        try
        {
            request.ContentLength = bytes.Length;
            os = request.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
        }
        catch
        { }
        string result = "";
    
        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
        Stream responseStream = webResponse.GetResponseStream();
        StreamReader responseStreamReader = new StreamReader(responseStream);
        result = responseStreamReader.ReadToEnd();
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题