【发布时间】:2011-10-30 14:31:38
【问题描述】:
我已经构建了一个 GPS 跟踪器,它可以更新 homepage 的位置(和网络摄像头图像)。
如何更新谷歌纬度用户的当前位置? 调用 curl 或 c 程序的简单 bash 脚本会很好!
更新:我还需要知道如何进行身份验证。
【问题讨论】:
标签: linux bash curl google-latitude
我已经构建了一个 GPS 跟踪器,它可以更新 homepage 的位置(和网络摄像头图像)。
如何更新谷歌纬度用户的当前位置? 调用 curl 或 c 程序的简单 bash 脚本会很好!
更新:我还需要知道如何进行身份验证。
【问题讨论】:
标签: linux bash curl google-latitude
当您说要“更新”谷歌纵横用户时,您想“更新他们的当前位置”,对吧?
对于某些 Google 服务,Google 将在 Google Code 上建立一个 Google API 项目。在这种情况下,您很幸运,因为有一个 Google Latitude API 描述了您可以使用 REST 接口执行的不同操作(REST 始终与 curl 兼容)。这是他们更新用户位置的示例代码:
POST https://www.googleapis.com/latitude/v1/currentLocation?key=INSERT-YOUR-KEY
/* Authorization header here */
Content-Type: application/json
{
"data": {
"kind":"latitude#location",
"latitude":37.420352,
"longitude":-122.083389,
"accuracy":130,
"altitude":35
}
}
Google Latitude API webside 描述了全部细节。您需要先获取 API 密钥,然后才能开始编写代码,并且需要进行 OATH 2.0 身份验证握手,然后才能真正更新用户的位置。
如果您不想自己编写身份验证代码,Google 在.NET, GWT, Java, PHP, Python, and Ruby 中提供了几个预打包的客户端库。它们都支持完整的 API,包括身份验证。
Google 有一个使用他们的 Java API 进行身份验证的完整示例。按照http://samples.google-api-java-client.googlecode.com/hg/latitude-json-oauth-sample/instructions.html?r=default 的说明进行操作并尝试一下。
【讨论】:
这就是你要找的吗?
在API Console 中,请务必在“服务”选项卡下请求访问纵横。该脚本将提示输入 API 密钥、客户端 ID、客户端密码,然后启动浏览器进行登录(您可能需要为您的系统调整该行,见下文)。登录并授予对应用程序的访问权限后,您将获得一个代码,您将在脚本提示时粘贴该代码。然后您将输入您的 lat/long/elev,这将被发布到服务中。
#!/bin/sh
LoginUrl="https://accounts.google.com/o/oauth2/auth"
TokenUrl="https://accounts.google.com/o/oauth2/token"
RedirectUri="urn:ietf:wg:oauth:2.0:oob"
Scope="https://www.googleapis.com/auth/latitude.all.best https://www.googleapis.com/auth/latitude.all.city https://www.googleapis.com/auth/latitude.current.best https://www.googleapis.com/auth/latitude.current.city"
CurlocUrl="https://www.googleapis.com/latitude/v1/currentLocation"
read -s -p "Enter your API Key: " APIKey
echo ""
read -s -p "Enter your Client ID: " ClientId
echo ""
read -s -p "Enter your Client Secret: " ClientSecret
echo ""
# this next line may need to be tweaked in order to launch the browser
open "${LoginUrl}?response_type=code&client_id=${ClientId}&redirect_uri=${RedirectUri}&scope=${Scope}"
read -s -p "Log in, grant permission, enter the code: " Code
echo ""
resp=`curl -is "${TokenUrl}" -d "code=${Code}&client_id=${ClientId}&client_secret=${ClientSecret}&redirect_uri=${RedirectUri}&grant_type=authorization_code"`
AccessToken=`echo "${resp}" | sed -e '/access_token/ !d; s/ *"access_token" *: *"\(.*\)",*/\1/'`
TokenType=`echo "${resp}" | sed -e '/token_type/ !d; s/ *"token_type" *: *"\(.*\)",*/\1/'`
ExpiresIn=`echo "${resp}" | sed -e '/expires_in/ !d; s/ *"expires_in" *: *"\(.*\)",*/\1/'`
RefreshToken=`echo "${resp}" | sed -e '/refresh_token/ !d; s/ *"refresh_token" *: *"\(.*\)",*/\1/'`
echo "Enter the location details."
read -p "Latitude in degrees (nn.nnnn): " latitude
read -p "Longitude in degrees (nn.nnnn): " longitude
read -p "Elevation in feed (nnnn): " altitude
curl -is "${CurlocUrl}" -H "Content-Type: application/json" -H "Authorization: OAuth ${AccessToken}" -d "{ 'data': { 'kind': 'latitude#location', 'latitude': '${latitude}', 'longitude': '${longitude}', 'accuracy': 0, 'altitude': ${altitude} } }"
【讨论】:
有一篇关于如何将 Curl 与 google 服务一起使用的条目:
http://code.google.com/apis/gdata/articles/using_cURL.html#other-tools
接下来,第一步是:
curl ^
-k ^
--proxy <your_proxy_here> ^
https://www.google.com/accounts/ClientLogin ^
--data-urlencode Email=hellonico@gmail.com ^
--data-urlencode Passwd=<cant_tell_you> ^
-d accountType=GOOGLE ^
-d source=Google-cURL-Example ^
-d service=lh2
这会给你返回类似的东西:
SID=<long_string_1>
LSID=<long_string_2>
Auth=<long_string_3>
现在您可以直接使用该令牌来验证和访问 Google 服务。以读取方式访问 Picassa 将是:
curl ^
--silent ^
--header "Authorization: GoogleLogin auth=<long_string_3>" ^
"http://picasaweb.google.com/data/feed/api/user/default"
并使用 PUT 或 POST 更新数据:
curl ^
--silent ^
--request POST ^
--data-binary "@template_entry.xml" ^
--header "Content-Type: application/atom+xml" ^
--header "Authorization: GoogleLogin auth=<long_string_3" ^
"http://picasaweb.google.com/data/feed/api/user/brad.gushue"
这同样适用于 Google 位置,您只需先请求 GoogleAPI,然后前往 documentation 解释如何发布数据。
POST 需要这样的东西:
POST https://www.googleapis.com/latitude/v1/location?key=INSERT-YOUR-KEY
/* Authorization header here */
Content-Type: application/json
{
"data": {
"kind":"latitude#location",
"timestampMs":"1274057512199",
"latitude":37.420352,
"longitude":-122.083389,
"accuracy":130,
"altitude":35
}
}
你去。
在这篇文章中,所有的 ^ 标记表示 EOL,用于将所有这些长命令拆分为多行。
【讨论】: