admin管理员组文章数量:1418380
I am trying to use the Google+ API to access Google Drive through a web application using javascript. I always get a 401 Invalid Credentials error, even though I have a valid access token that has not expired, (confirmed at ), I have also enabled Drive API, and have not revoked access to the token. Also, I have double checked and the access token is definitely sent in the authorization header (I looked at the request through my browser's debugger). From what I have researched, these are the main causes of a 401 Invalid Credentials error. However, I still get these errors and cannot figure out why. Here is my files, which I have removed the client_id and the api_key.
Javascript:
var SCOPES = ['.login',''];
var CLIENT_ID = 'MY_CLIENT_ID';
var API_KEY = 'MY_API_ID';
function load()
{
console.log("load");
gapi.client.setApiKey(API_KEY);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
console.log('checkAuth');
gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: false}, handleAuthResult);
}
function handleAuthResult(authResult) {
gapi.client.setApiKey(API_KEY);
if (authResult && !authResult.error)
{
makeRequest();
}
else
{
alert('error');
}
}
function makeRequest()
{
gapi.client.load('drive','v2',function()
{
var request = gapi.client.request({
'path': '/drive/v2/about',
'method': 'GET',
});
debugger;
request.execute(function(response)
{
debugger;
});
});
}
My HTML merely includes the relevant javascript files and calls the load method from myScript.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<script src="../js/myScript.js"></script>
<script src=".js"></script>
<script src=".js?onload=load"></script>
</head>
<body>
</body>
</html>
If anyone can figure out why I get a 401 error or how to further find information, that would be great.
EDIT: How should I create the request? When I use
var request = gapi.client.request({
'path': '/drive/v2/about',
'method': 'GET',
});
I can view that the authorization header is correct. However, in tutorials I have seen, they use:
var request = gapi.client.drive.about.get({});
In which the request looks different in my browser's debugger and does not necessarily contain the correct authorization header
I am trying to use the Google+ API to access Google Drive through a web application using javascript. I always get a 401 Invalid Credentials error, even though I have a valid access token that has not expired, (confirmed at https://www.googleapis./oauth2/v1/tokeninfo?access_token=MY_ACCESS_TOKEN), I have also enabled Drive API, and have not revoked access to the token. Also, I have double checked and the access token is definitely sent in the authorization header (I looked at the request through my browser's debugger). From what I have researched, these are the main causes of a 401 Invalid Credentials error. However, I still get these errors and cannot figure out why. Here is my files, which I have removed the client_id and the api_key.
Javascript:
var SCOPES = ['https://www.googleapis./auth/plus.login','https://www.googleapis./auth/drive'];
var CLIENT_ID = 'MY_CLIENT_ID';
var API_KEY = 'MY_API_ID';
function load()
{
console.log("load");
gapi.client.setApiKey(API_KEY);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
console.log('checkAuth');
gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: false}, handleAuthResult);
}
function handleAuthResult(authResult) {
gapi.client.setApiKey(API_KEY);
if (authResult && !authResult.error)
{
makeRequest();
}
else
{
alert('error');
}
}
function makeRequest()
{
gapi.client.load('drive','v2',function()
{
var request = gapi.client.request({
'path': '/drive/v2/about',
'method': 'GET',
});
debugger;
request.execute(function(response)
{
debugger;
});
});
}
My HTML merely includes the relevant javascript files and calls the load method from myScript.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<script src="../js/myScript.js"></script>
<script src="https://apis.google./js/client.js"></script>
<script src="https://apis.google./js/plusone.js?onload=load"></script>
</head>
<body>
</body>
</html>
If anyone can figure out why I get a 401 error or how to further find information, that would be great.
EDIT: How should I create the request? When I use
var request = gapi.client.request({
'path': '/drive/v2/about',
'method': 'GET',
});
I can view that the authorization header is correct. However, in tutorials I have seen, they use:
var request = gapi.client.drive.about.get({});
In which the request looks different in my browser's debugger and does not necessarily contain the correct authorization header
Share Improve this question edited Dec 30, 2013 at 0:09 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Sep 16, 2013 at 17:37 user2784790user2784790 131 silver badge4 bronze badges2 Answers
Reset to default 6I remember some issues where providing both an API Key and an Access Token caused problems with some API calls. Since the API Key isn't necessary when calling authenticated methods, can you try if it works if you remove the gapi.client.setApiKey(API_KEY);
calls (which you have in your code twice, but that shouldn't matter) to see if this is the case here?
Although I'm 90% sure that what Scarygami suggested will solve your issue - remove setApiKey from checkAuth - consider a few more things to try if that's not the source of the problem:
- Spaces in the client ID / client secret
- Does your authorized origin match what you have set up in the Google APIs console
If you put in a breakpoint at the
本文标签: javascriptGoogle API 401 Invalid CredentialsStack Overflow
版权声明:本文标题:javascript - Google+ API 401 Invalid Credentials - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283352a2651555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论