gapi.auth.authorize不适用于谷歌浏览器扩展程序

时间:2014-07-06 19:45:41

标签: google-chrome-extension oauth-2.0

以下是我的扩展程序的代码。我尝试使用Oauth2授权访问Google API,但是我收到错误消息" TypeError:无法读取属性'授权'未定义"当扩展名调用gapi.auth.authorize()函数时。关于可能出现什么问题的任何想法?

清单:

{
"manifest_version": 2,

"name": "name",
"description": "description",
"version": "1.0",

    "content_security_policy": "script-src 'self' https://apis.google.com/js/client.js; object-src 'self'",
"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
}
}

HTML:

<!doctype html>
<html>
  <head>    
    <script type="text/javascript" src="https://apis.google.com/js/client.js"></script>
    <script type="text/javascript" src="processform.js"></script>
  </head>
  <body>
    <form name="myform">
         First name: <input type="text" id="fname" name="firstname" /><br />
         Last name:  <input type="text" id="lname" name="lastname" /><br />
        <input name="Submit"  type="submit" value="Update" />
    </form>
  </body>
</html>

的javascript:

var CLIENT_ID = ' ... .apps.googleusercontent.com';
var SCOPES = [
  'https://www.googleapis.com/auth/drive.file',
];

function handleClientLoad() {
    checkAuth();
}

function checkAuth() {
    try {
        gapi.auth.authorize(
            {'client_id': CLIENT_ID, 'scope': SCOPES.join(' '), 'immediate': true},
            handleAuthResult);
    }
    catch(err) {
        var e = err.toString();
        alert(e);
    }
}


function handleAuthResult(authResult) {
    if (authResult) {
    } else {
        gapi.auth.authorize(
            {'client_id': CLIENT_ID, 'scope': SCOPES.join(' '), 'immediate': false},
            handleAuthResult);
}
}

function ExampleJS(){
 }


 document.addEventListener('DOMContentLoaded', function () {
  handleClientLoad();
});

 document.addEventListener('DOMContentLoaded', function () {
  document.forms["myform"].addEventListener('submit', ExampleJS);
});

1 个答案:

答案 0 :(得分:1)

您的content_security_policy应更新为"script-src 'self' https://apis.google.com/; object-src 'self'"。加载API会触发一个回调,该回调需要该单个URL之外的文件。您甚至可能需要进一步扩展安全策略,具体取决于授权需要下面瀑布的其他方面。

GAPI callbacks