Google chrome 在用户使用chrome.identity.launchWebAuthFlow登录后,如何在新的访问令牌到期之前获取该令牌?

Google chrome 在用户使用chrome.identity.launchWebAuthFlow登录后,如何在新的访问令牌到期之前获取该令牌?,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,对不起,如果以前有人问过这个问题,我已经花了一整天的时间在谷歌上搜索,没有任何结果 我正在使用Chrome Identity API作为我正在为我的工作构建的Chrome扩展(与GoogleSheets API交互),我希望为用户保留登录,但是找不到使用该API的刷新令牌 我首先实现了chrome.identity.getAuthToken(),并且我能够将请求授权给gSheets API,当访问令牌过期时问题就出现了。我知道,如果您使用interactive:false调用令牌,则再次调用ge

对不起,如果以前有人问过这个问题,我已经花了一整天的时间在谷歌上搜索,没有任何结果

我正在使用Chrome Identity API作为我正在为我的工作构建的Chrome扩展(与GoogleSheets API交互),我希望为用户保留登录,但是找不到使用该API的刷新令牌

我首先实现了
chrome.identity.getAuthToken()
,并且我能够将请求授权给gSheets API,当访问令牌过期时问题就出现了。我知道,如果您使用
interactive:false
调用令牌,则再次调用
getAuthToken()
会刷新令牌,但前提是您登录的令牌与您在与弹出窗口的初始交互中授权的电子邮件相同,而这不是我想要的,因为在我的浏览器中,我使用个人电子邮件登录,我想授权扩展的工作电子邮件。因此,1小时后,我需要请求一个新的令牌,它将提示登录弹出窗口,这是一个糟糕的用户体验

然后我尝试了
chrome.identity.launchWebAuthFlow()
,端点知道
launchWebAuthFlow()
适用于非Google帐户,尽管它工作得很好,但据我所知,没有办法在45分钟后刷新令牌,以避免将用户踢出

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  switch (request.type) {
    case "login":
      let authURL = "https://accounts.google.com/o/oauth2/v2/auth";
      const clientId = `<CLIENT_ID>`;
      const redirectURL = chrome.identity.getRedirectURL("oauth2");
      const auth_params = {
        client_id: clientId,
        redirect_uri: redirectURL,
        response_type: "token",
        scope: "https://www.googleapis.com/auth/spreadsheets"
      };

      const paramString = Object.keys(auth_params)
        .map(function(k) {
          return k + "=" + auth_params[k];
        })
        .join("&");

      authURL += "?" + paramString;

      chrome.identity.launchWebAuthFlow(
        { url: authURL, interactive: true },
        function(responseURL) {
          if (chrome.runtime.lastError) {
            console.log(chrome.runtime.lastError);
            return;
          }

          if (responseURL) {
            const paramsString = responseURL.substring(
              responseURL.indexOf("#") + 1
            );
            const params = new URLSearchParams(paramsString);

            const paramsObj = () => {
              const obj = {};
              for (var entry of params.entries()) {
                obj[entry[0]] = entry[1];
              }
              return obj;
            };

            const { access_token } = paramsObj();

            sendResponse({
              access_token
            });
          } else {
            console.log(`${responseURL} defined?`);
          }
        }
      );

      return true;
    default:
      console.log("placeholder");
      break;
      }
});
chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){
开关(请求类型){
案例“登录”:
让authURL=”https://accounts.google.com/o/oauth2/v2/auth";
常量clientId=`;
const redirectURL=chrome.identity.getRedirectURL(“oauth2”);
常量验证参数={
客户id:clientId,
重定向uri:redirectURL,
响应类型:“令牌”,
范围:“https://www.googleapis.com/auth/spreadsheets"
};
const paramString=Object.keys(auth_params)
.map(函数(k){
返回k+“=”+auth_参数[k];
})
.加入(“&”);
authURL+=“?”+参数字符串;
chrome.identity.launchWebAuthFlow(
{url:authURL,interactive:true},
功能(responseURL){
if(chrome.runtime.lastError){
log(chrome.runtime.lastError);
返回;
}
如果(负责人){
常量参数字符串=responseURL.substring(
响应索引(“#”)+1
);
const params=新的URLSearchParams(paramsString);
常量paramsObj=()=>{
常量obj={};
for(params.entries()的var条目){
obj[条目[0]]=条目[1];
}
返回obj;
};
const{access_token}=paramsObj();
发送响应({
访问令牌
});
}否则{
log(`${responseURL}已定义?`);
}
}
);
返回true;
违约:
console.log(“占位符”);
打破
}
});
如果这意味着我可以改善用户体验,我愿意放弃Chrome Identity API,使用另一个OAuth2流

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  switch (request.type) {
    case "login":
      let authURL = "https://accounts.google.com/o/oauth2/v2/auth";
      const clientId = `<CLIENT_ID>`;
      const redirectURL = chrome.identity.getRedirectURL("oauth2");
      const auth_params = {
        client_id: clientId,
        redirect_uri: redirectURL,
        response_type: "token",
        scope: "https://www.googleapis.com/auth/spreadsheets"
      };

      const paramString = Object.keys(auth_params)
        .map(function(k) {
          return k + "=" + auth_params[k];
        })
        .join("&");

      authURL += "?" + paramString;

      chrome.identity.launchWebAuthFlow(
        { url: authURL, interactive: true },
        function(responseURL) {
          if (chrome.runtime.lastError) {
            console.log(chrome.runtime.lastError);
            return;
          }

          if (responseURL) {
            const paramsString = responseURL.substring(
              responseURL.indexOf("#") + 1
            );
            const params = new URLSearchParams(paramsString);

            const paramsObj = () => {
              const obj = {};
              for (var entry of params.entries()) {
                obj[entry[0]] = entry[1];
              }
              return obj;
            };

            const { access_token } = paramsObj();

            sendResponse({
              access_token
            });
          } else {
            console.log(`${responseURL} defined?`);
          }
        }
      );

      return true;
    default:
      console.log("placeholder");
      break;
      }
});

TL;DR:我想在前一个令牌到期前几分钟,通过Chrome Identity API或任何其他OAuth2实现获得一个新的访问令牌,同时能够使用不同于已登录Chrome(人员)的Google帐户。

我本可以编辑前一个问题,但我将回答它:

有一种获取新令牌的方法,即使用
access\u type=offline
response\u type:“code”

let authURL = 'https://accounts.google.com/o/oauth2/v2/auth';
const redirectURL = chrome.identity.getRedirectURL("oauth2");
const auth_params = {
  client_id: clientId,
  redirect_uri: redirectURL,
  response_type: 'code',
  access_type: 'offline',
  scope: 'https://www.googleapis.com/auth/spreadsheets',
};
chrome.identity.launchWebAuthFlow({url: authURL, interactive: true}, responseURL => console.log(responseURL))
响应将是一个URL:

https://.chromiumapp.org/oauth2?code=4/A
#

返回的代码可以兑换为只能使用1次的令牌。然后向
https://www.googleapis.com/oauth2/v4/token
以以下代码段作为请求:

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

code=4/A<code>&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https://<extension_id>.chromiumapp.org/oauth2&
grant_type=authorization_code
POST/oauth2/v4/token HTTP/1.1
主持人:www.googleapis.com
内容类型:application/x-www-form-urlencoded
代码=4/A<代码>&
客户号=您的客户号&
客户机密=您的客户机密&
重定向\u uri=https://.chromiumapp.org/oauth2&
授权类型=授权代码
响应将是一个JSON对象,您可以使用它约1小时向Google API发出请求:

{
    "access_token": "<access_token>",
    "token_type": "Bearer",
    "expires_in": 3578
}
{
“访问令牌”:“,
“令牌类型”:“承载者”,
“到期日”:3578
}
最后,在访问令牌到期之前,您可以使用
interactive:false
调用
launchWebAuthFlow()
,它将返回一个新代码,并由此获得一个新令牌