我正在尝试构建一个访问 Google Drive 的 Chrome 扩展程序,其中 OAuth 2 身份验证请求是通过使用用于 javascript 的 Google API Client Libriary 执行的。我的扩展程序已在 Google Developers Console 中注册。
list .json
"background": {
"page": "background.html"
},
background.html
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="https://apis.google.com/js/client.js"></script>
<script type="text/javascript" src="google_auth/initiation.js"></script>
initiation.js
var CLIENT_ID = 'MY_CLIENT_ID';
var SCOPES = 'https://www.googleapis.com/auth/drive.file';
jQuery(window).load(function() {
gapi.auth.authorize({
client_id: CLIENT_ID,
scope: SCOPES,
immediate: false
}, function(authResult) {
if (authResult && !authResult.error) {
window.alert('Auth was successful!');
} else {
window.alert('Auth was not successful');
}
}
);
});
在浏览器中加载或重新加载扩展程序时,会出现一个弹出窗口,我会收到来自 Google 的响应消息:
这是一个错误。
错误:invalid_client
应用程序:MyExtension
实际上,我想知道为我的 Chrome 扩展程序选择 Google API Client Library 是否正确,我的实现是否遗漏了任何错误。
最佳答案
我没有足够的声誉来评论,但如果你的 initiation.js 是逐字的。您应该将“MY_CLIENT_ID”和 CLIENT_ID 替换为您在此处向 Google 注册您的应用程序时收到的客户端 ID https://console.developers.google.com/
它看起来像这样:
858545004388-tlfkq1hfdbuiductlb0qdvjds4s949gt.apps.googleusercontent.com
这就是 Error: invalid_client 告诉你的。
关于google-chrome - 使用 Google API 客户端库在 Chrome 扩展中进行 OAuth2 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306389/