Apps脚本抛出'无效状态令牌'经过同意屏幕上的授权后 [英] Apps script throws 'invalid state token' after autorization on consent screen

查看:44
本文介绍了Apps脚本抛出'无效状态令牌'经过同意屏幕上的授权后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望授权我的Web应用程序在用户的appfolder中创建一个文件夹来保存该应用程序的数据文件.

I wish to authorize my webapp to create a folder in the user's appfolder to hold the app's data files.

为此,我需要请求范围https://www.googleapis.com/auth/drive.appfolder

To do this, I need to request the scope https://www.googleapis.com/auth/drive.appfolder

到目前为止,我有以下代码:

So far I have the following code:

var CLIENT_ID            = '3941...';
var CLIENT_SECRET        = 'DY_P...';
var SCRIPT_ID            = '1XAF...';

var appfolder_scope     = 'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.appfolder';
var redirectURI         = 'https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2F'+ SCRIPT_ID + '%2Fusercallback';
var AuthEndpoint        = 'https://accounts.google.com/o/oauth2/v2/auth';



function getCallbackURL(callbackFunction) {

   var url = ScriptApp.getService().getUrl();      // Ends in /exec (for a web app)
   url = url.slice(0, -4) + 'usercallback?state='; // Change /exec to /usercallback
   var stateToken = ScriptApp.newStateToken()
        .withMethod(callbackFunction)
        .withTimeout(120)
        .createToken();
   return url + stateToken;
}


function generateAuthRequestURL() {
  var AuthRequest = AuthEndpoint;
  var Query = '?'
      + 'scope=profile%20' + appfolder_scope
      + '&state=' + getCallbackURL(cb)
      + '&redirect_uri=' + redirectURI
      + '&response_type=code'
      + '&client_id=' + CLIENT_ID
    //+ '&login_hint=...%40gmail.com'
      ;
  AuthRequest += Query;
  Logger.log(AuthRequest);      
  return AuthRequest;
}

function cb(response) {
  Logger.log(response);
}

当我单击由generateAuthRequestURL()生成的URL时,它将带我进入同意屏幕,在该屏幕上单击允许".但是,每当我收到状态令牌无效或已过期"时.

When I click on the url generated by the generateAuthRequestURL() it takes me to the consent screen where I click allow. But then every time I get 'The state token is invalid or has expired'.

该Webapp已发布,并且我已经测试了exec和dev版本,结果相同.我也尝试过使用或不使用login_hint.

The webapp is published and I have tested both the exec and dev versions with the same result. I have also tried with and without a login_hint.

我还尝试了 Apps-Script-Folder-Library 以及 gdrive-appdata .我无法使第一个工作,而第二个我甚至都不知道如何使用.

I have also experimented with Apps-Script-Folder-Library as well as gdrive-appdata. I couldn't get the first one to work, and the second one I don't even know how to use.

推荐答案

基于此

Based from this thread, make sure that you've put the project key into the related field. This error may occur when the key changed after making a copy of the script.

您也可以检查此问题,以确保您正在使用正确的项目密钥.

You can also check on this issue which suggested to ensure that you are using the right project key.

以下一些相关论坛也可能会有所帮助:

Here are some related forums which might also help:

  • Issues with OAuth 2.0 Library for Google Apps Scripts
  • OAuth2 support through library returns The state token is invalid or has expired. Please try again

这篇关于Apps脚本抛出'无效状态令牌'经过同意屏幕上的授权后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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