Gadget OAuth2 Authorization

Refresh

December 2018

Views

521 time

3

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);
 }                  

2 answers

1

Рамки Gadgets API не поддерживает oauth2. Задачи API поддерживает OAuth1 однако, так что вы все еще может быть в состоянии получить все заработало.

1

Ответ - с помощью OAuth вместо OAuth 2.0.

OAuth секция должна быть:

 <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>

Запрос [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;

Чтобы использовать OAuth1 для Google Tasks API нам нужен ключ API .