Gmail API:在没有用户交互的情况下无法应用身份验证令牌

时间:2020-04-30 17:59:16

标签: javascript oauth-2.0 gmail-api

OS:Windows 10, 浏览器:Chrome v.81, 伺服器:PHP 5.6, jQuery:版本2.1.1

应用程序:Browser Quickstart

客户端希望该浏览器应用程序自动更新用户的令牌,而不必每次都强制用户手动重新授权。

根据快速入门页面,“在初始用户授权后,使用即时:真模式的对gapi.auth.authorize的调用将获得无需用户干预的auth令牌。”

我即将使其工作。我可以授权并收集新的auth令牌,但是找不到正确的语法来将该令牌应用于更新的登录序列。这就是我到目前为止所拥有的。


    // Initializes the API client library and sets up sign-in state listeners.

    function initClient() {
        gapi.client.init({
          apiKey: API_KEY,
          clientId: CLIENT_ID,
          discoveryDocs: DISCOVERY_DOCS,
          scope: SCOPES
        }).then(function () {
          // Listen for sign-in state changes.
          gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

          // Handle the initial sign-in state.
          updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
          authorizeButton.onclick = handleAuthClick;
          signoutButton.onclick = handleSignoutClick;
        }, function(error) {
          appendPre(JSON.stringify(error, null, 2));
        });
      }

    //Checks user's login state

    function updateSigninStatus(isSignedIn) {
        if (isSignedIn) {
          authorizeButton.style.display = 'none';
          signoutButton.style.display = 'block';

          //This is the API interaction I want to run.
          displayInbox();

        } else {

//I think this is where the automatic token refresh would happen (when the user
//is no longer logged in), which is why I've added in the "autoRefresh" function.

          autoRefresh();

          authorizeButton.style.display = 'block';
          signoutButton.style.display = 'none';
        }
      }

//My Auto Refresh Function, to get the new token without client interaction

    function autoRefresh() {
        gapi.auth.authorize({
          'client_id':CLIENT_ID,
          'immediate':'true',
          'scope':SCOPES
        }).then(function(response) {
            console.log(response);
            var access_token = response.access_token;
            console.log(access_token);

// This is where I'm stuck. I have the token, but idea how to feed it back to the 
// API, to actually renew the client's authentication.
// The only server-side example is in Python, which I can't use (client's locked into PHP).
// Before you ask, the Client's PHP server is unable to run the GMAIL API from PHP. 
// All my hopes rest on vanilla javascript... So,
// How do I authorize the client with the new access_token from here?

        });
      }

      /**
       *  These are the button functions. I've added them in just to be thorough. I'm trying 
       *  to replace the "handleAuthClick(event)" with my "autoRefresh()" function.
       */

      function handleAuthClick(event) {
        gapi.auth2.getAuthInstance().signIn();
      }

      function handleSignoutClick(event) {
        gapi.auth2.getAuthInstance().signOut();
      }

如您所见,我即将重新授权客户端,我只是不知道将令牌提交回API服务器进行重新授权的最后一步。

该教程的“身份验证”按钮的代码(gapi.auth2.getAuthInstance()。signIn();)似乎要求用户手动登录,并且在我的生命中,我找不到与之等效的代码通过auth2进行身份验证,而无需用户登录。

我希望时间不会太长,在此先感谢您的时间和帮助。

任何帮助,链接或建议,将不胜感激。

祝你有美好的一天!

0 个答案:

没有答案