问题
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.com/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.com/auth/plus.login','https://www.googleapis.com/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.com/js/client.js"></script>
<script src="https://apis.google.com/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
回答1:
I 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?
回答2:
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
来源:https://stackoverflow.com/questions/18834017/google-api-401-invalid-credentials