0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Himanshu Jain
  • 518
  • 4
  • 20
  • In this i am using the concept of threading also. In this multiple threads can access the google calendar for access token. Is it related to this issue – Himanshu Jain Nov 07 '13 at 07:14

1 Answers1

0

Have a look at this question Youtube API C# OAuth token exchange for access_token returns invalid request

It looks to be a similar problem.

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks for your reply. Actually i am sending the correct request but once in a 50 execution i am getting the exception of 403 Forbidden. Otherwise it is running fine. I have tried the process that you told me but still getting the same problem. I am getting 403 exception when i am sending too many request using threading. – Himanshu Jain Nov 08 '13 at 05:59
  • It may well be that the 403 forbidden is actually a throttling exception. I have similar problems with Drive giving me a 403 Rate Limit Exceeded. I have to slow my Drive transactions to < one per 3 seconds in order to avoid the problem – pinoyyid Nov 08 '13 at 06:25
  • how can i check that it is a rate limit exception or something else. So shall i do the sleep of thread if it throws an exception?. – Himanshu Jain Nov 08 '13 at 06:37
  • all I can suggest is to slow down the API calls and see if the problem is resolved. When you get a 403, do you exponentially backoff and retry, and does the retry work? – pinoyyid Nov 08 '13 at 07:16
  • Event after using exponentially backoff still getting the same problem. Actually i am having 20 accounts and in every account i am having 20 calendars so means in a single shot i am executing 400 calendars (400 execution to Google calendar) so sometimes it goes fine and some times it throw exception. all the 20 accounts are in threads. – Himanshu Jain Nov 08 '13 at 07:46
  • Sounds a lot like the problem I have trying to do multiple concurrent Drive updates. The only solution I found was to slow down my rate of API calls. – pinoyyid Nov 08 '13 at 11:08
  • Now i will try to do some sleep threading in my code. Thanks a lot for your help. – Himanshu Jain Nov 08 '13 at 11:40