0
votes

I have code to retrieve a list of events from Google's Calendar API. After retrieving those events, it checks the list against another local list I have and populates the Google Calendar events with any from the local list which aren't already in there.

The first part runs completely fine, where it will retrieve an access token, then use that token in a request to get the events list. The next part, when it tries to upload an event fails, with a 401 "Invalid Credentials" error message.

I've checked everything in the console API to make sure Google Calendar is enabled as an API, and that everything is set up properly in there. I've checked the API documentation to ensure I'm definitely using the correct scope in my authentication request for the API post query to submit the event to GCal.

At this point I'm not sure if it's an issue with the request and the messaging is just extremely misleading, or if the problem is genuinely with authorisation, and I'm just missing something else entirely.

request = new RestRequest("/calendars/"+calID+"/events", Method.POST);
request.AddParameter("access_token", access_token);
request.AddJsonBody(import);

IRestResponse<GCalEvent> inner_response = client.Execute<GCalEvent>(request);

In the code above, the access_token variable is the same value which was used seconds before in the successful request to retrieve the events. (i.e. It should be valid.)

The "import" variable specified is based on a custom object model included below:

public class GCalEvent
{
   public string id { get; set; }
   public string status { get; set; }
   public string htmlLink { get; set; }
   public DateTime created { get; set; }
   public DateTime updated { get; set; }
   public string summary { get; set; }
   public string description { get; set; }
   public string start { get; set; } // yyyy-MM-dd
   public string end { get; set; } // yyyy-MM-dd
}

Any help or suggestions would be very welcome, as I've been brick-walling on this for a while now.

1
I would use a sniffer to see if I could get more info. Here is some things to check. 1) See if the response is using http 1.0 (stream mode) or 1.1 (chunk mode) 2) Check language country settings in http headers 3) Check in the response the Browser type (IE or chrome). Different machines may have default browser set different 4) Check Certificates on machines to make sure the correct certificate is installed and not expired. - jdweng
Traditionally, access tokens are passed as authorization headers. - Maximilian Burszley

1 Answers

0
votes

maybe you need to add your access token like this request.AddHeader("Authorization", "Bearer " + access_token);