Search code examples
oauthoauth-2.0google-calendar-apigoogle-gadgetgoogle-tasks-api

Gadget OAuth2 Authorization


I am developing a gadget (html, JS) to run inside (Google Calendar page). I need to show user's tasks there so I need a connection to Google Task Api. I need to use OAuth2 authorization and such requests as:

GET https://www.googleapis.com/tasks/v1/users/@me/lists 
GET https://www.googleapis.com/tasks/v1/lists/tasklist/tasks

Unfortunately I havent found description or samples for gadgets withOAuth2` authorization.

Could you please tell me what will the OAuth2 section look like in this case?

 <OAuth2>
   <Service name="[service_name]">
      <Authorization url="https://.../authorize"/>
      <Token url="https://.../oauth2/token"/>
   </Service>
 </OAuth2>

Could you please approve this code for a request? (JS)

 function loadContents(){           
   var url = "https://www.googleapis.com/tasks/v1/users/@me/lists?alt=json";            
   var params = {};
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
    params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.OAUTH2;
   params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;     
   params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] ="[service_name]";       
   var callback = function (response) { 
     if (response.oauthApprovalUrl) {
       ...                 
     }                      
   };             
   gadgets.io.makeRequest(url, callback, params);
 }                  

Solution

  • The answer is - using OAuth instead of OAuth 2.0.

    OAuth section should be:

     <OAuth>
          <Service name="google">
            <Access url="https://www.google.com/accounts/OAuthGetAccessToken" method="GET" /> 
            <Request url="https://www.google.com/accounts/OAuthGetRequestToken?scope=https://www.googleapis.com/auth/tasks" method="GET" /> 
            <Authorization url="https://www.google.com/accounts/OAuthAuthorizeToken?oauth_callback=http://oauth.gmodules.com/gadgets/oauthcallback" /> 
          </Service>
        </OAuth>
    

    The request is [JS]:

     var params = {};
     url = "https://www.googleapis.com/tasks/v1/users/@me/lists?key=YOUR_API_KEY";
          params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
          params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
          params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
          params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
          params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
    

    To use OAuth1 for Google Tasks API we need an API key.