【问题标题】:How to get access token for google oauth?如何获取 google oauth 的访问令牌?
【发布时间】:2012-08-03 17:15:41
【问题描述】:

我正在使用 C# (ASP.NET)。我想使用 Google OAuth 访问我的应用程序中的用户个人资料详细信息。我成功获得了授权码,但在获取访问令牌时遇到了问题。 我更喜欢Google tutorials。在教程中,我读到我必须发送请求并从谷歌获得响应。为此,我使用System.Net.HttpWebRequest/HttpWebResponse(我是否走对了路)。我用过这段代码...

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("http://accounts.google.com");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;

Stream strm = req.GetRequestStream();
strm.Write(buffer, 0, buffer.Length);
strm.Close();

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Response.Write(((HttpWebResponse)resp).StatusDescription);

但是,我得到了错误:

远程服务器返回错误:(405) Method Not Allowed。

更新:这里的变量code是授权码。

【问题讨论】:

  • @user854301 我可以参考这个,但我想知道HttpWebRequest/Response 的使用是否正确?我可以从HttpWebRequest 向谷歌发送请求吗?
  • 你的缓冲区中的“代码”是什么??
  • @Apoorva 这是一个授权码。
  • 如何获得授权码...你能告诉我我不知道吗..

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


【解决方案1】:
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;
}

【讨论】:

  • 如何从 google 获取验证码?。我想使用你的代码,所以我不必使用 key.json 文件,只需为我的客户端 ID 和客户端密码使用应用程序配置。如果你能分享如何获取授权代码,那就太棒了。
【解决方案2】:

由于我在实现 Google auth 的过程中遇到了类似的问题,所以我将发布有效的代码。最后提到的问题:错误(400)错误请求可能是由前导“?”引起的在上面的代码中..

 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

【讨论】:

  • @Sahil,确保 oauth url 是有效的。请注意,我在大约 5 年前使用过这个代码 sn-p。
  • @Cyber​​Ninja 在同一个论坛中查看我的代码。它工作完美。如果有任何其他问题,请告诉我。
  • @SahilBhatia 我的意思是您如何获得作为参数传递的代码,例如您如何通过 .net 代码从 google 请求该代码?我知道您可以通过 url 作为浏览器中的 request_type 请求,但它会要求我登录。
  • 我尝试过类似的方法,但反应不佳,有人可以帮忙吗?
【解决方案3】:

最初的请求似乎有些过时了。但我发现 Google 的代码示例包含许多“最佳实践”的管家代码,这些代码很难与基本操作分开。

我最近发布了一个将所有 REST 操作表示为 curl 命令的文档。很难精通每种语言,但 curl 似乎是通用的。大多数人都知道 - 否则,它很容易掌握。在我的 curl 示例中,-d 标志表示 POST 操作。否则,参数将附加到 URL。

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

【讨论】:

    【解决方案4】:

    我的代码正在运行,我在上面两行中犯了错误。应该是这样的

    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");
    

    剩下的代码是正确的。

    【讨论】:

    • @Viktor:我注意到您的 client idclient secret 都包含在缓冲区中。我的托管网站没有 SSL。您知道 Google 是否会接受来自非 SSL 连接的网络请求?显然,这不安全,而且有人可能会窃听我的凭据,但我不知道如何绕过它。
    • 我停止阅读“没有 SSL”。使用 SSL。
    【解决方案5】:

    我认为您将 POST 请求发送到错误的端点,正确的是 https://accounts.google.com/o/oauth2/token

    【讨论】:

    • 我使用这个,但它显示了其他错误。现在错误是The remote server returned an error: (400) Bad Request.我哪里出错了?
    • 深入了解异常的细节,完整的错误信息会告诉你你的请求有什么问题
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2023-03-27
    • 2021-09-12
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多