1
votes

I'm trying to connect my asp.net (vb.net) app to google calendar so I can post events on google calendars.

So far, I've managed to get an access token, and refresh token. I plan on storing the refresh token so my app can access a particular google calendar long after the user has logged out.

So, i've got my access token and refresh token, now what? I can't find any examples where i can connect to google calendar api v3 with just an access token - all the examples, even on googles site, involve using the client library to get an access token (which i've already got), and creating a new CalendarService() object with no parameters passed.

Is it possible to create a CalendarService class and give it an access token i already have?

1

1 Answers

1
votes

It took a while to figure out the oauth workflow so I created a small project that had the basic concepts coded

  1. Get access via oauth
  2. refresh expired token
  3. do some event using the access token

The project is hosted here https://code.google.com/p/csharp-googlecalendar-api-v3-oauth2/ and its in csharp BUT I did make it as simple as I could

You will probably want to look at this specific file https://code.google.com/p/csharp-googlecalendar-api-v3-oauth2/source/browse/CalChecker/simpleEvent.aspx.cs

string accesstoken = "usersaccesstoken";
string client_id = "getclientidfromcloud";
string client_secret = "getclientsecretfromcould";
string useremailaccount = "[email protected]";

var httpWebRequest1 = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/calendar/v3/calendars/" + WebUtility.HtmlEncode(useremailaccount) + "/events");
httpWebRequest1.ContentType = "text/json";
httpWebRequest1.Method = "GET";
httpWebRequest1.Headers["Authorization"] = "Bearer " + accesstoken;
var httpResponse1 = (HttpWebResponse)httpWebRequest1.GetResponse();
using (var streamReader = new StreamReader(httpResponse1.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

All the code is doing is creating a GET WebRequest, notice the Authorization Header.. Thats how you pass the access token to call an event that requires Auth.

Hope it helps