【问题标题】:Get access token from google oauth from code从代码中获取来自 google oauth 的访问令牌
【发布时间】:2013-03-22 14:48:04
【问题描述】:

我使用以下代码从以下代码中获取访问令牌

String code = HttpContext.Current.Request["code"];
                    string redirecturl = HttpContext.Current.Request["url"];                                  

                    string Url = "https://accounts.google.com/o/oauth2/token";
                    string grant_type = "authorization_code";
                    string redirect_uri_encode = UrlEncodeForGoogle(url);
                    string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type={5}";

                    HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
                    string result = null;
                    request.Method = "POST";
                    request.KeepAlive = true;
                    request.ContentType = "application/x-www-form-urlencoded";
                    string param = string.Format(data, code,configurationInfo.oauthclientid , configurationInfo.oauthclientsecretid, redirect_uri_encode, grant_type, "offline");

                    var bs = Encoding.UTF8.GetBytes(param);
                    using (Stream reqStream = request.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                    }

                    using (WebResponse response = request.GetResponse())
                    {
                        var sr = new StreamReader(response.GetResponseStream());
                        result = sr.ReadToEnd();
                        sr.Close();

                    }

我得到了回应

 The remote server returned an error: (400) Bad Request.

我不知道我哪里出错了

等待您宝贵的cmets

【问题讨论】:

  • 你已经解决了这个问题吗?
  • @cherhan ya 如果我们正确提供所有注册详细信息,它将起作用
  • 是的,我也有一段和你类似的代码,很高兴听到它有效。

标签: c# .net dotnetopenauth google-oauth


【解决方案1】:

Google 还提供了一个更高级别的库来访问其服务。我发现使用它的 API 变得更加容易。

http://code.google.com/p/google-api-dotnet-client/

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2015-02-24
    • 2012-08-03
    • 1970-01-01
    • 2011-09-04
    • 2012-02-12
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多