当来自 Google 的访问令牌过期时,我正在尝试生成新的访问令牌。但是在我的生产服务器上,我得到了下面提到的异常:
Exception:
at System.Net.HttpWebRequest.GetResponse() at System.Net.HttpWebRequest.GetResponse() at
GoogleCalendarProvider_V3.ExchangeCodeWithAccessAndRefreshToken(String RefreshToken)
Exception Message:
The remote server returned an error: (403) Forbidden.
我正在创建一个 http Web 请求以获取访问令牌。请检查以下代码
string Url = "https://accounts.google.com/o/oauth2/token";
string grant_type = "refresh_token";
string data = "client_id={0}&client_secret={1}&refresh_token={2}&grant_type={3}";
HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
string result = string.Empty;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
string param = string.Format(data, ClientID, ClientSecret, RefreshToken, grant_type);
request.Credentials = CredentialCache.DefaultCredentials;
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();
}
请在这种情况下帮助我。