1

I've been reading lots of documentation about Google API access and OAuth flow using it but I don't seem to get it working in my mind, so I want to get some help first in order to have a clear idea about how it works then I can code it using the corresponding API.

What I want to achieve is feed a Java application running in a PC with specific Google user data, like localization through Google Latitude API. In order to get this, OAuth must be used, so I need getting the user consent, then access the user data from the application running in my computer, and I don't know how to manage this.

I've already registered my application with the Google APIs Console and enabled the Google Latitude module. I've also tried the Latitude console application here and it works properly (a browser tab opened asking for a Google user; I entered it and I got the location data), but I'm having problems when trying to adapt the program flow to my needs.

In my application, the 'remote' user is supposed to send a request (a custom JSON message) to the server asking for service enable/disable, like allowing the server to track his/her position through Latitude. Then, AFAIK, the server should send to the user a URL so the user can give the consent, but I don't know how to get this URL and how the server realizes about this consent and gets the token (automatically? Google tracks this authorization process?). Once my server gets the specific user token, then I should be ready to get service data for that user using the received token.

As I said before, I've tried according to different references, but as the documentation seems to be really scattered and much of it is already deprecated, I've been unable to get it working.

4

1 回答 1

0

从您的描述来看,安装的应用程序 OAuth2 流程似乎适合您。

在某个时候,大概当用户安装您的桌面应用程序时,您应该启动一个浏览器——嵌入在您的应用程序中或默认浏览器中——并将它们发送到这个Google OAuth2 端点。在您的请求中,填写文档要求的所有参数:Latitude API 范围、client_id 等。Google 作为授权服务器,将负责用户身份验证、会话选择和用户同意。如果用户向您的 API 授予对她数据的访问权限,您将在浏览器窗口的标题中或在 localhost 端口处收到一个授权代码。

获得代码后,您可以将其交换为访问令牌和刷新令牌。访问令牌是您调用 API 和访问用户数据所需的。虽然它是短暂的 - 检查响应中的 expired_in 参数,我相信它是 3600 秒。- 因此,您需要定期使用您的长期刷新令牌 ping 令牌端点并将其交换为访问令牌。

您可以在上面链接的文档中找到对此流程的更全面的描述。

于 2013-01-10T02:52:28.530 回答