Hello group,

We resolve ourselves to write our own mini client to SimpleGeo as
Google AppEngine constraints leave us no choice.

But as simple as it is, the Python code I wrote (which is pasted in
full below) returns 401 Unautorized responses all the time. And I was
wondering what can it be that I'm doing wrong and have other eyes take
a look at it.

Thanks a lot in advance for helping.

=========

import hmac
import random
import urllib
import httplib
import hashlib
import binascii
import timestamp


HOST = 'api.simplegeo.com'
REALM = 'http://api.simplegeo.com'
METHOD = 'GET'
SIG_METHOD = 'HMAC-SHA1'


def url_path(layer, lat, lon):
    path = '/0.1/records/%s/nearby/%s,%s.json' % (layer, lat, lon)
    return path


def url_params(day):
    params = {
        'limit': 150,
        'radius': 0.025,
        'end': timestamp.day_end(day),
        'start': timestamp.day_start(day),
    }
    return urllib.urlencode(sorted(params.items()))


def hmac_sha1_signature(path, params, secret):
    raw = '&'.join(('GET', path, params))
    hashed = hmac.new(secret, raw, hashlib.sha1)

    # calculate the digest base 64
    return binascii.b2a_base64(hashed.digest())[:-1]


def oauth_header(url_path, url_params):
    apikey = 'GECfebmXCMSvPFk3tK2CePJf4HVMkvvU'
    secret = 'YX6fj6wqJLKZBjRsFhqup3cMTgxkWPpB'

    signature = hmac_sha1_signature(url_path, url_params, secret)
    #signature = plaintext_signature(secret)

    params = [
        'OAuth realm="%s"' % REALM,
        'oauth_nonce="%08d"' % random.randint(0, 100000000),
        'oauth_timestamp="%s"' % timestamp.now(),
        'oauth_consumer_key="%s"' % apikey,
        'oauth_signature_method="%s"' % SIG_METHOD,
        'oauth_signature="%s"' % signature,
        'oauth_version="1.0"',
    ]

    hd = {'Authorization': ', '.join(params)}
    print hd
    return hd


def nearby(layer, lat, lon, day):
    conn = httplib.HTTPConnection(HOST, 80)
    path = url_path(layer, lat, lon)
    params = url_params(day)
    header = oauth_header(path, params)
    header.update({'Content-Type':'application/x-www-form-urlencode'})
    url = path+'?'+params
    print url
    conn.request(METHOD, url, body=None, headers=header)
    return conn


if __name__ == '__main__':
    import datetime
    c = nearby(
        'com.simplegeo.global.twitter',
        '39.3932481', '-76.6109441',
        datetime.date.today()
    )
    r = c.getresponse()
    print r.status, r.reason

-- 
You received this message because you are subscribed to the Google Groups 
"OAuth" group.
To post to this group, send email to oa...@googlegroups.com.
To unsubscribe from this group, send email to 
oauth+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/oauth?hl=en.

Reply via email to