0

我正在为 ASP.NET 中的 Google 集成开发我的。我正在尝试使用访问令牌和刷新令牌创建服务。但我无法获得刷新令牌,因为我刚刚获得访问令牌、令牌类型和过期。请检查以下代码。

 private String ExchangeCodeWithAccessAndRefreshToken()
{
    string Url = "https://accounts.google.com/o/oauth2/token";
    string grant_type = "authorization_code";
    string redirect_uri_encode = UrlEncodeForGoogle(Convert.ToString(Session["URL"]));
    string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}";
    string Code = Request.QueryString["Code"];
    HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
    string result = null;
    request.Method = "POST";
    request.KeepAlive = true;
    request.ContentType = "application/x-www-form-urlencoded";
    String ClientID = ConfigurationManager.AppSettings["clientID"].ToString();
    String ClientSecret = ConfigurationManager.AppSettings["clientSecret"].ToString();
    string param = string.Format(data, Code, ClientID, ClientSecret, redirect_uri_encode, grant_type);
    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();
    }
    var jsonSerializer = new JavaScriptSerializer();
    var tokenData = jsonSerializer.Deserialize<GoogleTokenModel>(result);
    return tokenData.Access_Token;
}
4

1 回答 1

0

尝试在您的 URL 中设置access_type=offline :

string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type=offline";
于 2013-08-05T13:08:44.317 回答