【问题标题】:Unable to get Refresh Token in Google Calendar Version 3无法在 Google 日历版本 3 中获取刷新令牌
【发布时间】:2013-08-03 06:02:27
【问题描述】:

我正在为 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;
}

【问题讨论】:

    标签: c# asp.net google-calendar-api


    【解决方案1】:

    尝试在您的网址中设置access_type=offline

    string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type=offline";
    

    【讨论】:

    • 感谢您的回复,但我通过指定 access_type=offline&approval_prompt=force 解决了这个问题,之前我使用的是 access_type=offline&approval_prompt=auto
    • 我还想知道一件事,因为我正在从版本 2 迁移到版本 3,所以我无法使用版本 2 的刷新令牌进行任何操作,所以还有其他替代方法吗这样我就可以使用我已经存在的刷新令牌。
    • 这确实应该是一个新问题,因为它是一个不同的主题。
    猜你喜欢
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2019-10-26
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多