Search code examples
linuxbashcurlgoogle-latitude

Update google latitude


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.


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