简体   繁体   中英

Google Calendar API v3: using the AccessToken to retrieve the CalendarList

I am trying to access the Google API v3 Calendarlist but I keep getting 401/404 responses even though I have a valid AccessToken for the User.
(I completed the OAuth2 protocol for Web Server Applications and received the https://www.googleapis.com/auth/calendar permission)

It's been rather difficult to find good documentation for this, the google site itself isn't very helpful and searches are full of old v2 information or use SDKs. Usually I can make do with translating python replies to c# but in this case even that didn't help me.

So I'm pulling my hair out but I'm probably forgetting something very simple here like a missing parameter or somesuch, so it would be greatly appreciated if someone would take a quick look at this code and tell me what I'm doing wrong.

var applicationKey = moduleModel.Application.Key;
var userID = moduleModel.User.UserID;
var accessToken = moduleModel.User.AccessToken.Token;

// It doesn't seem to make any difference whether I use the querystring
// parameter or the request header to specify the access_token
var sendAccessTokenAsHeader = 
    !(Request.QueryString["SendAccessTokenAsHeader"] == "0");

// I tried navigating to a specific user but it results in a 404 Not Found
var requestSpecificUserID = 
    Request.QueryString["RequestSpecificUserID"] == "1";

// Including or omitting my API_KEY doesn't seem to make any difference
var sendApplicationKey = 
    !(Request.QueryString["SendApplicationKey"] == "0");

var urlBuilder = new System.Text.StringBuilder();

urlBuilder.Append("https://");
urlBuilder.Append("www.googleapis.com");

if (requestSpecificUserID)
{
    urlBuilder.Append
        (string.Format("/calendar/v3/users/{0}/calendarList", userID));
}
else
{
    urlBuilder.Append
        ("/calendar/v3/users/me/calendarList");
}

var parameterJoiner = "?";

if (sendApplicationKey)
{
    urlBuilder.Append
    (
        string.Format
        (
            "{0}{1}={2}",
            parameterJoiner,
            "key",
            HttpUtility.UrlEncode(applicationKey)
        )
    );
    parameterJoiner = "&";
}

if (!sendAccessTokenAsHeader)
{
    urlBuilder.Append
    (
        string.Format
        (
            "{0}{1}={2}",
            parameterJoiner,
            "access_token",
            HttpUtility.UrlEncode(accessToken)
        )
    );
    parameterJoiner = "&";
}

var httpWebRequest = HttpWebRequest.Create(urlBuilder.ToString()) 
    as HttpWebRequest;
httpWebRequest.CookieContainer = new CookieContainer();

if (sendAccessTokenAsHeader)
{
    httpWebRequest.Headers["Authorization"] = string.Format
    (
        "Bearer {0}",
        accessToken
    );
}

// GetSafeResponse is just an extension method to catch the WebException 
// when the HttpStatusCode != OK

var response = httpWebRequest.GetSafeResponse(); 

// returns 401 (requestSpecificUserID = false)
// or 404 (requestSpecificUserID = true)

var responseText = response.GetResponseText();

return responseText;

Thank you in advance!

I found my answer by reading this question.

Apparently, even though the Google documentation does not mention it at all , the parameter minAccessRole is required.

Working example:

var accessToken = moduleModel.User.AccessToken.Token;

var urlBuilder = new System.Text.StringBuilder();

urlBuilder.Append("https://");
urlBuilder.Append("www.googleapis.com");
urlBuilder.Append("/calendar/v3/users/me/calendarList");
urlBuilder.Append("?minAccessRole=writer");

var httpWebRequest = HttpWebRequest.Create(urlBuilder.ToString()) 
    as HttpWebRequest;

httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.Headers["Authorization"] = 
    string.Format("Bearer {0}", accessToken);

var response = httpWebRequest.GetSafeResponse();

var responseText = response.GetResponseText();

return responseText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM