ホームページをその位置(およびWebカメラの画像)で更新するGPSトラッカーを作成しました。
Google Latitudeユーザーの現在の場所を更新するにはどうすればよいですか?curlまたはcプログラムを呼び出す単純なbashスクリプトがあれば便利です。
更新:認証の方法も知っておく必要があります。
ホームページをその位置(およびWebカメラの画像)で更新するGPSトラッカーを作成しました。
Google Latitudeユーザーの現在の場所を更新するにはどうすればよいですか?curlまたはcプログラムを呼び出す単純なbashスクリプトがあれば便利です。
更新:認証の方法も知っておく必要があります。
Google Latitudeユーザーを「更新」したい場合は、「現在地を更新」したいですよね?
一部のGoogleサービスでは、GoogleはGoogleCodeでGoogleAPIプロジェクトを設定します。この場合、幸運なことに、RESTインターフェースを使用して実行できるさまざまなアクションを記述したGoogle Latitude APIがあります(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
}
}
GoogleLatitudeAPIのウェブサイドに詳細が記載されています。コードの記述を開始する前にAPIキーを取得する必要があります。また、ユーザーの場所を実際に更新する前に、OATH2.0認証ハンドシェイクを実行する必要があります。
認証コードを自分で記述したくない場合、Googleは、.NET、GWT、Java、PHP、Python、およびRubyでいくつかのパッケージ化されたクライアントライブラリを提供します。それぞれ、認証を含む完全なAPIをサポートしています。
Googleには、JavaAPIを使用して認証を行う完全な例があります。http://samples.google-api-java-client.googlecode.com/hg/latitude-json-oauth-sample/instructions.html?r=defaultの指示に従って、試してみてください。
これはあなたが探しているものですか?
APIコンソールで、必ず[サービス]タブでLatitudeへのアクセスをリクエストしてください。このスクリプトは、APIキー、クライアントID、クライアントシークレットの入力を求め、ログイン用のブラウザを起動します(システムに合わせてその行を微調整する必要がある場合があります。以下を参照してください)。ログインしてアプリケーションへのアクセスを許可すると、スクリプトのプロンプトが表示されたときに貼り付けるコードが表示されます。次に、サービスに投稿される緯度/経度/標高を入力します。
#!/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} } }"
GoogleサービスでCurlを使用する方法に関するエントリがあります。
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を要求してから、データの投稿方法を説明しているドキュメントにアクセスする必要があります。
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を意味し、これらの長いコマンドをすべて複数行に分割するために使用されます。