403访问令牌时禁止例外

时间:2013-11-05 10:35:53

标签: google-calendar-api access-token webrequest

我正在尝试在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();
        }

请在这方面帮助我。

1 个答案:

答案 0 :(得分:0)

看一下这个问题Youtube API C# OAuth token exchange for access_token returns invalid request

这看起来是一个类似的问题。