3

Gmail 用の Chrome 拡張機能を構築し、Chrome Auth を使用して、この StackOverflow の投稿Gmail API ドキュメントなどに従って gmail api にアクセスしようとしています。トークンを正常に受信しましたが、トークンをchrome.identity.getAuthToken({ 'interactive': true }, function(token) {}リクエスト URL にプラグインすると、次の 401 エラー応答が返されます (コードは次のとおりです)

エラー応答

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "required",
        "message": "Login Required",
        "locationType": "header",
        "location": "Authorization"
      }
    ],
    "code": 401,
    "message": "Login Required"
  }
}

私のコード:
background.js

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {
    chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
      thisToken = token
      chrome.runtime.onMessage.addListener(
        function(request,sender,sendResponse){
          var gapiRequestUrlAndToken = "https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"

          var makeGetRequest = function (gapiRequestURL)
            {
                var xmlHttp = new XMLHttpRequest();
                xmlHttp.open( "GET", gapiRequestURL, false );
                xmlHttp.send( null );
                return xmlHttp.responseText;
            }

          makeGetRequest(gapiRequestUrlAndToken);
        }
      );
    });
  }
})

マニフェスト.json

{
  "manifest_version": 2,
  "key": "<key>",
  "name": "exampleName",
  "description": "Description",
  "version": "0.0.1.7",
  "default locale": "en",
  "icons": { "128": "imgs/pledge_pin.png"},
  "content_scripts" : [
    {
      "matches": ["*://mail.google.com/mail/*"],
      "js": ["js/jquery.js", "js/compose.js", "bower_components/jqnotifybar/jquery.notifyBar.js"],
      "css": ["css/stylesheet.css", "bower_components/jqnotifybar/css/jquery.notifyBar.css"]
    }
  ],
  "background": {
    "scripts": ["scripts/background.js"]
  },
  "permissions": [
    "identity"
  ],
  "oauth2": {
    "client_id": "<client id>",
    "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
  }
}

Chrome Auth for Gmail api を使用しようとしているという事実と関係があると思われますが、私が読んだ他の投稿では、これが実行可能なオプションであると信じています。

私のコードがそれを与えなかった場合に備えて、私は初心者なので、どんな助けも大歓迎です.

4

1 に答える 1

2

key一般的なアプリケーション シークレット用です。ユーザー固有のトークンの場合は、access_token. また、トークンは でラップしないでください{}。が URL で使用している実際の値である場合mail%40gmail.com、機能しません。認証されたユーザーの電子メール アドレスまたは のいずれかである必要がありますme

だから変更:

"https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"

これに:

"https://www.googleapis.com/gmail/v1/users/me/threads?access_token=" + thisToken
于 2015-07-05T14:39:08.253 に答える