无法获取Google API的刷新令牌.节点js [英] can't get refresh token for google api. Node js

查看:114
本文介绍了无法获取Google API的刷新令牌.节点js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可通过Google API远程运行的Apps脚本脚本.当我转到链接时,它为我提供了用于检索访问令牌的代码,它表示所请求的文件不存在.但是,无论如何,代码都在url中,这给了我访问令牌,但没有刷新令牌.

I've got an Apps Script script that I am running remotely through Google's API. When I go to the link it gives me for the code to retrieve the access token, it says that the file requested does not exist. However, the code is in the url anyway, this gives me the access token, but not the refresh token.

我尝试添加'access_type:offline'和'approval_prompt:force',但是这些都没有改变.这是代码:

I have tried adding 'access_type: offline' and 'approval_prompt: force', but those didn't change anything. Here's the code:

var { google } = require('googleapis');
var fs = require('fs');
var async = require('async');
const readline = require('readline');




// If modifying these scopes, delete token.json.
const SCOPES = [
  "https://www.googleapis.com/auth/drive",
  "https://www.googleapis.com/auth/script.projects",
  "https://www.googleapis.com/auth/spreadsheets"

];
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
fs.readFile('./credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  // Authorize a client with credentials, then call the Google Apps Script API.
  authorize(JSON.parse(content), callScriptFunction);
});

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
  //   const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2("294862899955-bb0929ato2qem8cqllggrpuqpqit191v.apps.googleusercontent.com", "Ds4-q0G3QZog4UamQrc3HFrW", "https://script.google.com/oauthcallback");

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getAccessToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
 * @param {getEventsCallback} callback The callback for the authorized client.
 */
function getAccessToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES,
  });
  console.log('Authorize this app by visiting this url:', authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return console.error('Error retrieving access token', err);
      oAuth2Client.setCredentials(token);
      // Store the token to disk for later program executions
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}

function callScriptFunction(auth) {
  var scriptId = "MwnuiFwldt-0ZLyLnoi0Q5kfoO49Cn6ao";
  var script = google.script('v1');

  script.scripts.run({
    auth: auth,
    resource: {
      function: 'automateSheet',
      parameters: [
        process.argv[2],
        process.argv[3],
        process.argv[4],
        process.argv[5],
        process.argv[6],
        process.argv[7],
        process.argv[8],
      ]
    },
    scriptId: scriptId,
    devMode: true
  }, function (err, resp) {
    if (err) {
      console.log(err);
    }
    else {
      var r = resp.data;
      if ("error" in r) {
        console.log("Error: %o", r.error);
      } else {
        console.log("Result: %o", r.response.result);
      }
    }
  });
}

当我同意允许该应用访问我的帐户时,这是Google给我的页面:

Here's the page Google gives me when I agree to allow the app access to my account:

推荐答案

我在这里找到了答案:

未收到Google OAuth刷新令牌

那个家伙说要去 https://myaccount.google.com/u/2/permissions?pli=1&pageId=none

然后撤消您的应用权限.它为我工作.因为每次第一次之后刷新令牌都不会出现

then revoke your apps permission. it worked for me. because every time after the first time, the refresh token does not appear

这篇关于无法获取Google API的刷新令牌.节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆