代码拉取完成,页面将自动刷新
const db = require('@arangodb').db;
const google_session = db._collection('google_session');
const aql = require('@arangodb').aql;
const request = require('@arangodb/request');
const jsrsasign = require('jsrsasign');
const iisGoogle = require('./config').iisGoogle;
const privatKey = require('./config').privatKey;
function authorize() {
// Header
const oHeader = {alg: 'RS256', typ: 'JWT'};
// Payload
let oPayload = {};
const tNow = jsrsasign.jws.IntDate.get('now');
const tEnd = jsrsasign.jws.IntDate.get('now + 1hour');
oPayload.iss = iisGoogle;
oPayload.scope = "https://www.googleapis.com/auth/spreadsheets";
oPayload.aud = "https://www.googleapis.com/oauth2/v4/token";
oPayload.exp = tEnd;
oPayload.iat = tNow;
const sHeader = JSON.stringify(oHeader);
const sPayload = JSON.stringify(oPayload);
const sJWT = jsrsasign.jws.JWS.sign("RS256", sHeader, sPayload, privatKey);
const getToken = request(
{
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: sJWT
}
});
if (getToken && getToken.json && getToken.json.access_token) {
const token = `${getToken.json.token_type} ${getToken.json.access_token}`;
google_session.truncate();
google_session.insert({
accessToken: token,
exp: oPayload.exp,
iat: oPayload.iat
}
);
return token
} else {
return {error: true, getToken, sJWT}
}
}
function getAutorise () {
const dbToken = db._query(aql`
FOR session IN google_session
FILTER session.exp > (DATE_NOW()/1000 + 60)
RETURN session.accessToken
`);
if (dbToken._documents[0]) {
return dbToken._documents[0]
} else {
return authorize()
}
}
module.exports = {
authorize,
getAutorise
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。