Home:ALL Converter>403 forbidden exception in accessing access token

403 forbidden exception in accessing access token

Ask Time:2013-11-05T18:35:52         Author:Himanshu Jain

Json Formatter

I am trying to generate a new access token when access token from Google is expired. But On my production server i am getting below mentioned exception:

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.

I am creating a http web request in order to get the access token. please check the below code for this

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();
        }

please help me in this context.

Author:Himanshu Jain,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19786909/403-forbidden-exception-in-accessing-access-token
yy