如何将 google api access_token 从 java 服务器传递到 gapi javascript 客户端?
How can I pass a google api access_token from java server to gapi javascript client?
我可以在 Java 运行 中使用 AuthorizationCodeFlow 在 Tomcat 中获取凭据。
现在我正在尝试将我在服务器上获得的访问令牌传递回客户端,这样我们就可以使用 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",
...
}
我在多个地方看到人们表示他们已经这样做了。但到目前为止,我无法使它正常工作。
在 fiddler 中我得到以下响应:
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 无效,因此显示 "Invalid Credentials" 消息。
我通过将以下内容放入邮递员来测试 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"
}
我可以在 Java 运行 中使用 AuthorizationCodeFlow 在 Tomcat 中获取凭据。
现在我正在尝试将我在服务器上获得的访问令牌传递回客户端,这样我们就可以使用 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",
...
}
我在多个地方看到人们表示他们已经这样做了。但到目前为止,我无法使它正常工作。
在 fiddler 中我得到以下响应:
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 无效,因此显示 "Invalid Credentials" 消息。
我通过将以下内容放入邮递员来测试 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"
}