0

我正在使用由 jersey rest 公开的谷歌云数据存储,我已经使用 oauth2 进行了身份验证以访问 rest APIS。

现在我想从我的 gmail 插件中调用 restful API 来填充 gmail 侧边栏上的一些 lovs。

我已经尝试过https://github.com/googlesamples/apps-script-oauth2上提供的 oauth2 身份验证。

如何使用 gmail 插件在 gmail 侧边栏上显示authorizationUrl ?

代码库

appscript.json

{
  "timeZone": "America/Los_Angeles",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Gmail",
      "serviceId": "gmail",
      "version": "v1"
    }],
    "libraries": [{
      "userSymbol": "OAuth2",
      "libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF",
      "version": "24",
      "developmentMode": true
    }]
  },
  "oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/gmail.addons.current.message.metadata", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/script.storage"],
  "gmail": {
    "name": "Gmail Add-on Quickstart",
    "logoUrl": "https://www.gstatic.com/images/icons/material/system/2x/bookmark_black_24dp.png",
    "contextualTriggers": [{
      "unconditional": {
      },
      "onTriggerFunction": "buildAddOn"
    }],
    "primaryColor": "#4285F4",
    "secondaryColor": "#4285F4",
    "openLinkUrlPrefixes": ["https://mail.google.com/"],
    "version": "TRUSTED_TESTER_V2"
  }
}

代码.gs

function buildAddOn(e) {
  var driveService = createOauthService();
  Logger.log('Access'+driveService.hasAccess());     // GETTING FALSE HERE
  Logger.log('Auth URL'+driveService.getAuthorizationUrl()); // GETTING PROPER AUTH URL.
  Logger.log('Token'+driveService.getAccessToken()); // NEED TOKEN TO BE USED ON REST CALL
}

function createOauthService() {
  return OAuth2.createService('drive')
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')
      .setClientId('MY CLOUD CONSOLE CLIENT ID')
      .setClientSecret('MY SECREAT')
      .setCallbackFunction('authCallback')
      .setPropertyStore(PropertiesService.getUserProperties())
      .setScope('https://www.googleapis.com/auth/drive')
      .setParam('login_hint', Session.getActiveUser().getEmail())
      .setParam('access_type', 'offline');
      .setParam('approval_prompt', 'force');
}

这是正确的方式还是我错过了什么?

请建议。

4

1 回答 1

0

一种记录在案的方法是从您的 buildAddOn() 函数返回一张卡片,其中包含一个配置为启动授权序列的按钮。

function buildAuthorizeCard() {
  var authorizationUrl = getOAuthService().getAuthorizationUrl();
  var card = CardService.newCardBuilder();
  var section = CardService.newCardSection();
  section.addWidget(CardService.newTextButton()
          .setText("Authorize MyService")
          .setAuthorizationAction(
                CardService.newAuthorizationAction()
                  .setAuthorizationUrl(authorizationUrl)
          ));
  card.addSection(section);
  return card.build();
}

你可以试试看。

于 2017-12-02T22:41:28.077 回答