为Chrome扩展程序运行identity.getAuthToken时出现错误“此帐户的服务已被禁用”

时间:2018-07-29 08:01:58

标签: google-chrome-extension oauth-2.0 google-api google-sheets-api google-oauth2

我正在尝试创建一个扩展程序,该扩展程序使用所选数据并将其保存到Google表格中。虽然听起来很简单,但我仍然停留在Auth2部分。

这是我到目前为止所做的:

我创建了一个manifest.json,并将其上传到chrome开发人员仪表板上,以获取“密钥”和“ id”。

使用“ id”获取身份验证客户端ID,并将其添加到我的manifest.json中。

接下来,我在“ popup.html”中添加了基本html,在“ options.js”中添加了上下文菜单代码部分,在“ manifest.json”中将其定义为后台脚本。

接下来,我使用“ getAuthToken”在“ popup.js”中获取令牌。当我运行扩展程序时,一切正常,但是一旦我单击按钮以获取令牌,什么都没有发生,并且返回“ undefined”令牌值。错误消息是

  

运行identity.getAuthToken时未选中的runtime.lastError:OAuth2请求失败:服务响应并显示错误:“此帐户的服务已被禁用。”

Manifest.json

{
"name":"ext1",
"manifest_version":2,
"version": "1.0",
"description":"",
"browser_action":{
    "default_icon":"icon48.png",
    "default_popup":"popup.html"
},

"permissions":[
"identity",
"https://*/*",
"http://*/*",
"contextMenus",
"https://accounts.google.com/o/oauth2/token",
"storage"
],
"background":{
    "scripts":["options.js"],
    "persistent":false
    },
"content_security_policy":"script-src 'self' https://apis.google.com; object-src 'self'",
"oauth2":{
    "client_id":"<>.apps.googleusercontent.com",
    "scopes":[
    "https://www.googleapis.com/auth/spreadsheets"
    ]
},
"key":"<>"}

Popup.html

 <!doctype <!DOCTYPE html>
<html>
<head>
    <title>text</title>
    <script src="jquery-3.3.1.min.js"></script>
    <script src="popup.js"></script>

</head>

<body>
<h1>random text</h1>
<h2> Data:  <span id="data"></span></h2>
<input type="submit" id="button" value="Authorize">
</body>
</html>

popup.js

$(function(){
    chrome.storage.sync.get('datas', function(values){
        $('#data').text(values.datas);
    })
    $("#button").on("click",function(){
        chrome.identity.getAuthToken({"interactive":true},function(token){
        alert("getting token......")  
        console.log("token")
        console.log(token);
        })
    })
})

我曾尝试搜索类似的问题,但没有一个能解决我的问题。请帮助我做错了。

3 个答案:

答案 0 :(得分:1)

听起来您正在使用已启用Google advanced protection的帐户,该帐户禁用了非Google应用的gmail和与驱动器相关的API访问权限:

  

为了确保数据安全,大多数非Google应用和服务将无法访问您Google帐户中的某些数据,例如Google Drive或Gmail。 *

答案 1 :(得分:0)

您可以参考此link中的建议以使其起作用:

  
      
  1. 转到Google Developers Console并创建一个新项目。
  2.   
  3. 转到面板中的 API和身份验证>凭据,以创建新的客户端ID。如果您的应用已上传,则可以从Chrome Developer Dashboard获取chrome应用的应用ID。
  4.   
  5. 转到“ API和身份验证”>“同意”屏幕,然后填写电子邮件地址和产品名称并保存。
  6.   

答案 2 :(得分:0)

使用另一个Google帐户登录对我有用,并解决了该问题。