我能够使用在Tomcat中运行的Java中的AuthorizationCodeFlow获取凭据。
现在我正在尝试将我在服务器上获得的访问令牌传递回客户端,以便我们能够使用 gapi javascript 客户端在浏览器中工作,而无需通过我们的服务器。
我使用以下方法在 gapi 中设置访问令牌:
function setAccessToken() {
gapi.auth.setToken({
access_token: '<access_token from server>'
});
}
然后,我调用 gapi.client.load 来获取驱动器客户端代码:
gapi.client.load('drive', 'v2', onDriveClientLoaded);
然后,我调用一个函数来获取或创建一个在Google云端硬盘中创建一个文件夹(这已经使用gapi.auth.authorize()进行了测试,因此我知道代码应该可以正常工作。
function onDriveClientLoaded() {
getOrCreateFolder();
}
但是在getOrCreateFolder中,当我调用gapi.client.drive.children.list(...)时,响应是
{
code: 401,
message: "Invalid Credentials",
...
}
我见过很多地方的人表示他们已经这样做了。 但到目前为止,我无法做到这一点。
在小提琴手中,我得到以下响应:
HTTP/1.1 401 Unauthorized
Vary: Origin
Vary: X-Origin
WWW-Authenticate: Bearer realm="https://accounts.google.com/", error=invalid_token
Content-Type: application/json; charset=UTF-8
Date: Fri, 01 May 2015 20:16:22 GMT
Expires: Fri, 01 May 2015 20:16:22 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=1
Content-Length: 249
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
以下是略微编辑的请求:
GET https://content.googleapis.com/drive/v2/files/root/children?q=title%20%3D%20%27TestFolder%27 HTTP/1.1
Host: content.googleapis.com
Connection: keep-alive
Authorization: Bearer <access_token from server>
X-Goog-Encode-Response-If-Executable: base64
X-Origin: http://localhost:8080
X-ClientDetails: appVersion=5.0%20(Windows%20NT%206.1)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F42.0.2311.90%20Safari%2F537.36&platform=Win32&userAgent=Mozilla%2F5.0%20(Windows%20NT%206.1)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F42.0.2311.90%20Safari%2F537.36
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-JavaScript-User-Agent: google-api-javascript-client/1.1.0-beta
X-Referer: http://localhost:8080
Accept: */*
X-Chrome-UMA-Enabled: 1
X-Client-Data: CKu1yQEIhLbJAQijtskBCKm2yQEIxLbJAQiiicoBCImSygE=
Referer: https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.OuOrzZ8GQcU.O%2Fm%3D__features__%2Fam%3DIQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCO9uGNmv6wlfXBJwCbYRCdcqx94WQ
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "dM4Z0GasI3ekQlrgb3F8B4ytx24/sCBysa-CxrZGnYdsTvea7yHHhJ0"
事实证明,一切都按预期工作。我需要查看令牌的刷新,因为我使用的access_token无效,因此出现"凭据无效"消息。
我通过将以下内容放入邮递员来测试access_token:
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=<access_token>
使用不正确的access_token时,我收到无效的凭据响应,但是当我刷新令牌时,我得到了以下有效响应:
{
"issued_to": "<some_id>.apps.googleusercontent.com",
"audience": "<some_id>.apps.googleusercontent.com",
"scope": "https://www.googleapis.com/auth/drive",
"expires_in": 3470,
"access_type": "offline"
}