can't get refresh token for google api. Node js

让人想犯罪 __ 提交于 2019-12-25 02:27:08

问题


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.

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

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


回答1:


i found the answer here:

Not receiving Google OAuth refresh token

where the guy said to go to 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



来源:https://stackoverflow.com/questions/52600586/cant-get-refresh-token-for-google-api-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!