Question

I have built a GPS tracker that updates a homepage with its positions (and webcam images).

How can I update a the current location of a google latitude user? A simple bash script invoking curl or a c-program would be nice!

Update: I also need to know how to do the authentication.

Était-ce utile?

La solution

Is this what you're looking for?

In the API Console, be sure to request access to Latitude under the Services tab. This script will prompt for API Key, Client ID, Client Secret and then launch a browser for login (you may need to tweak that line for your system, see below). Once you're logged in and grant access to your application, you'll get a code you'll paste in when prompted by the script. Then you'll enter your lat/long/elev which will be posted to the service.

#!/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} } }"

Autres conseils

When you say you want to "update" a Google Latitude user, you want to "update their current location", right?

For some Google services, Google will set up a Google API project on Google Code. In this case, you're in luck, because there is a Google Latitude API that describes the different actions you can do using a REST interface (REST is always compatible with curl). Here's their example code for updating a user's location:

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
    }
}

The Google Latitude API webside describes the full details. You'll need to get an API key before you can start writing code, and you'll need to do an OATH 2.0 authentication handshake before you can actually update the user's location.

Update

If you don't want to write the authentication code yourself, Google provides several pre-packaged client libraries, in .NET, GWT, Java, PHP, Python, and Ruby. They each support the full API, including authentication.

Google has a full example that uses their Java API to do authentication. Follow the instructions at http://samples.google-api-java-client.googlecode.com/hg/latitude-json-oauth-sample/instructions.html?r=default and try it out.

There is an entry on how to use Curl with google services:

http://code.google.com/apis/gdata/articles/using_cURL.html#other-tools

Following this, the first step would be:

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

This will returns you something like:

SID=<long_string_1>
LSID=<long_string_2>
Auth=<long_string_3>

Now you can use that token directly to authenticate and access Google Services. Accessing Picassa in read, would be:

curl ^
 --silent ^
 --header "Authorization: GoogleLogin auth=<long_string_3>" ^    
 "http://picasaweb.google.com/data/feed/api/user/default"

And to update data, using a PUT or 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" 

The same applies to Google Location, you just need to ask for a GoogleAPI first and then head to the documentation explaining how to post data.

The POST needs something like this:

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
 }
}

There you go.

In this post, all the ^ marks, means EOL, and are used to split all those long commands over multiple lines.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top