11

我正在使用 C# (ASP.NET)。我想使用 Google OAuth 访问我的应用程序中的用户个人资料详细信息。我成功获得了授权码,但在获取访问令牌时遇到了问题。我更喜欢谷歌教程。在教程中,我读到我必须发送请求并从谷歌获得响应。为此,我使用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是授权码。

4

5 回答 5

8

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

于 2012-08-06T20:05:14.763 回答
4

由于我在实现谷歌身份验证的过程中遇到了类似的问题,所以我将发布有效的代码。最后提到的问题:错误(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
于 2012-09-18T20:05:14.393 回答
2

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

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

剩下的代码是正确的。

于 2012-09-21T08:08:08.137 回答
0

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

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

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

于 2013-01-05T20:17:59.087 回答
0
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;
}
于 2017-07-04T18:44:48.763 回答