Javascript 谷歌应用程序脚本中的Ouath2

Javascript 谷歌应用程序脚本中的Ouath2,javascript,google-apps-script,google-oauth,Javascript,Google Apps Script,Google Oauth,我已经非常仔细地遵循了关于为google服务设置Oauth2令牌的指南,但是我仍然无法让access令牌工作 我收到的错误是访问权限未授予或已过期。(第454行,文件“服务”,项目“OAuth2”) 注意*我的项目已经被列入GMB API库的白名单,我已经在API控制台中启用了它。我还从我的项目中检索到了ClientID和ClientSecret //Oauth 2 Flow Start function getGmbService() { // Create a new service w

我已经非常仔细地遵循了关于为google服务设置Oauth2令牌的指南,但是我仍然无法让access令牌工作

我收到的错误是访问权限未授予或已过期。(第454行,文件“服务”,项目“OAuth2”)

注意*我的项目已经被列入GMB API库的白名单,我已经在API控制台中启用了它。我还从我的项目中检索到了ClientID和ClientSecret

//Oauth 2 Flow Start
function getGmbService() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth2.createService('gmb')
  // Set the endpoint URLs, which are the same for all Google services.
  .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')

  // Set the client ID and secret, from the Google Developers Console.
  .setClientId('...')
  .setClientSecret('...')

  // Set the name of the callback function in the script referenced
  // above that should be invoked to complete the OAuth flow.
  .setCallbackFunction('authCallback')

  // Set the property store where authorized tokens should be persisted.
  .setPropertyStore(PropertiesService.getUserProperties())
  .setCache(CacheService.getUserCache())

  // Set the scopes to request (space-separated for Google services).
  .setScope('https://www.googleapis.com/auth/business.manage')

  // Below are Google-specific OAuth2 parameters.

  // Sets the login hint, which will prevent the account chooser screen
  // from being shown to users logged in with multiple accounts.
  .setParam('login_hint', Session.getActiveUser().getEmail())

  // Requests offline access.
  .setParam('access_type', 'offline')

  // Forces the approval prompt every time. This is useful for testing,
  // but not desirable in a production application.
  .setParam('approval_prompt', 'force');
}

function showSidebar() {
  var gmbService = getGmbService();
  if (!gmbService.hasAccess()) {
    var authorizationUrl = gmbService.getAuthorizationUrl();
    var template = HtmlService.createTemplate(
      '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
      'Reopen the sidebar when the authorization is complete.');
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    DocumentApp.getUi().showSidebar(page);
  } else {
    Logger.log("No Access")
  }
}

function authCallback(request) {
  var gmbService = getGmbService();
  var isAuthorized = gmbService.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

//Oauth2 Flow Finish

function testRequest() {
  var gmbService = getGmbService();

  var payload = {
    "pageSize": 5
  }

  var options = {
    "headers": {
      "Authorization": 'Bearer ' + gmbService.getAccessToken()
    },
    "method": 'GET',
    "payload": payload,
    "muteHttpExceptions": true
  };


  var response = UrlFetchApp.fetch("https://mybusiness.googleapis.com/v4/accounts",options)
  Logger.log(response);

}
//Oauth 2流启动
函数getGmbService(){
//使用给定名称创建新服务。在
//持久化授权令牌,以确保其在
//属性存储的范围。
返回OAuth2.createService('gmb'))
//设置端点URL,这与所有Google服务相同。
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
//从Google开发者控制台设置客户端ID和密码。
.setClientId(“…”)
.setClientSecret(“…”)
//在引用的脚本中设置回调函数的名称
//应该调用上面的函数来完成OAuth流。
.setCallbackFunction('authCallback')
//设置应在其中保留授权令牌的属性存储。
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
//设置要请求的作用域(谷歌服务用空格分隔)。
.setScope('https://www.googleapis.com/auth/business.manage')
//下面是特定于Google的OAuth2参数。
//设置登录提示,这将阻止帐户选择器屏幕
//从显示到使用多个帐户登录的用户。
.setParam('login_hint',Session.getActiveUser().getEmail())
//请求脱机访问。
.setParam('访问类型','脱机')
//每次强制批准提示。这对测试非常有用,
//但在生产应用中并不理想。
.setParam(“批准提示”、“强制”);
}
函数showSidebar(){
var gmbService=getGmbService();
如果(!gmbService.hasAccess()){
var authorizationUrl=gmbService.getAuthorizationUrl();
var template=HtmlService.createTemplate(
'. ' +
“授权完成后重新打开侧栏。”);
template.authorizationUrl=authorizationUrl;
var page=template.evaluate();
DocumentApp.getUi().showSidebar(第页);
}否则{
Logger.log(“无访问权限”)
}
}
函数authCallback(请求){
var gmbService=getGmbService();
var isAuthorized=gmbService.handleCallback(请求);
如果(未授权){
返回HtmlService.createHtmlOutput('成功!您可以关闭此选项卡');
}否则{
返回HtmlService.createHtmlOutput('Denied.youcan close this tab');
}
}
//Oauth2流动抛光
函数testRequest(){
var gmbService=getGmbService();
var有效载荷={
“页面大小”:5
}
变量选项={
“标题”:{
“授权”:“承载者”+gmbService.getAccessToken()
},
“方法”:“获取”,
“有效载荷”:有效载荷,
“muteHttpExceptions”:true
};
var response=UrlFetchApp.fetch(“https://mybusiness.googleapis.com/v4/accounts“,选项)
Logger.log(响应);
}

你有没有得到过这个问题的答案。我也有同样的问题