After getting auth token with required permissions, I am trying to access user’s tasks.
Request URL:
https://www.googleapis.com/tasks/v1/lists/%40default/tasks?key={YOUR_API_KEY}
Code to Make request:
public void FetchTasks(string url)
{
var httpWebRequest = HttpWebRequest.CreateHttp(url);
httpWebRequest.BeginGetResponse(new AsyncCallback(FinishedWebRequest), httpWebRequest);
}
private void FinishedWebRequest(IAsyncResult ar)
{
var httpWebRequest = ar.AsyncState as HttpWebRequest;
var httpWebResponse = (HttpWebResponse) httpWebRequest.EndGetResponse(ar);
byte[] responseByteArray= new byte[200];
httpWebResponse.GetResponseStream().Read(responseByteArray, 0, responseByteArray.Length);
}
Response
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
Do I need anything else as part of request headers along with auth token in the URL?
You need to set the
AuthorizationHTTP header to a validaccess_tokenfor the user whose tasks you’d like to access. Assuming you’ve completed the OAuth 2.0 dance and have a validaccess_token, you can set the header by changingFetchTasks()to something like this:Also, it looks like you’re accessing the APIs using HTTP directly. Managing OAuth 2.0 tokens can be a bit cumbersome, You may want to investigate using the official Google .NET API client library which takes care of a lot of the heavy lifting for you.