自动OAuth2令牌无法使用 - Google Apps脚本 [英] Automated OAuth2 token not working - Google Apps Script

查看:326
本文介绍了自动OAuth2令牌无法使用 - Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在文档中运行Google Apps脚本,该脚本会自动发送带有附加Google电子表格的电子邮件作为.xlsx文件,每隔几小时运行一次。



以下是如果我使用来自Google OAuth2操场的手动OAuth2代码的解决方案:

  function downloadXLS ){
var AUTH_TOKEN =xxxx;
var auth =AuthSub token = \+ AUTH_TOKEN +\;
var file = Drive.Files.get('xxxx');
var response = UrlFetchApp.fetch('https://docs.google.com/spreadsheets/d/xxx/export?format=xlsx',{headers:{Authorization:auth}});
var doc = response.getBlob();
app = DriveApp.createFile(doc).setName(file.title +'.xls')
MailApp.sendEmail(xxx@xxx.com,oh man,Body,{附件:app})
}

尝试自动生成授权令牌所有步骤都在这里:



https: //github.com/googlesamples/apps-script-oauth2



更改脚本:
.setClientId('...')
.setClientSecret('...')
我还将URI中的项目ID放入 https://script.google.com/macros/d/myprojectkey/usercallback 的Google开发者控制台



但是当我运行函数makeRequest()它告诉我:访问未授予或过期

所以我想知道我错过了哪一步。



你有什么线索是什么继续?



非常感谢帮助,
感谢

解决方案

您需要执行第2步:将用户指向授权网址

侧栏加载时您将单击该链接并打开Oauth对话框。在您允许访问后,您可以使用getAccessToken()方法。



编辑:
对于您的特定情况,您不需要单独的OAuth流程。您可以使用Apps脚本获取您需要执行导出的令牌。由于您已请求访问驱动器,您的令牌可用于导出调用。

 函数downloadXLS(){

var file = Drive.Files.get('xxxx');
var response = UrlFetchApp.fetch('https://docs.google.com/spreadsheets/d/xxx/export?format=xlsx',{headers:{Authorization:Bearer+ ScriptApp.getOAuthToken() }});
var doc = response.getBlob();
app = DriveApp.createFile(doc).setName(file.title +'.xls')
MailApp.sendEmail(xxx@xxx.com,oh man,Body,{附件:app})
}


I am trying to run a google apps script, in a document, that sends an email with an attached google spreadsheet as an .xlsx file automatically, running every few hours.

Below is the solution that works if I use a manual OAuth2 code coming from the google OAuth2 playground :

function downloadXLS() {
  var AUTH_TOKEN = "xxxx"; 
  var auth = "AuthSub token=\"" + AUTH_TOKEN + "\"";
  var file = Drive.Files.get('xxxx');
  var response = UrlFetchApp.fetch('https://docs.google.com/spreadsheets/d/xxx/export?format=xlsx',{headers: {Authorization: auth}});      
  var doc = response.getBlob();
  app = DriveApp.createFile(doc).setName(file.title + '.xls')
  MailApp.sendEmail("xxx@xxx.com", "oh man", " Body", { attachments: app })
}    

To try to auto-generate the authorization token I followed exactly all the steps here:

https://github.com/googlesamples/apps-script-oauth2

Changing on the script : .setClientId('...') .setClientSecret('...') I also put in the URI the the Project Id inside the https://script.google.com/macros/d/myprojectkey/usercallback of the google developer console

But when i run the function makeRequest() it tells me : "Access not granted or expired"

So i wonder which step i missed.

Do you have any clue on what is going on ?

Help is much appreciated, Thanks

解决方案

You need to do step 2: Direct the user to the authorization URL
When the sidebar loads you will click the link and the Oauth dialog will open. After you allow access you can use the getAccessToken() method.

EDIT: For your specific case you do not need a separate OAuth flow. You can use Apps Script to get the token you need to do that export. As you are already requesting access to drive your token will work for the export call.

function downloadXLS() {

  var file = Drive.Files.get('xxxx');
  var response = UrlFetchApp.fetch('https://docs.google.com/spreadsheets/d/xxx/export?format=xlsx',{headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});      
  var doc = response.getBlob();
  app = DriveApp.createFile(doc).setName(file.title + '.xls')
  MailApp.sendEmail("xxx@xxx.com", "oh man", " Body", { attachments: app })
}    

这篇关于自动OAuth2令牌无法使用 - Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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