Skip to content

Instantly share code, notes, and snippets.

@nderkach
Created December 18, 2014 09:57
Show Gist options
  • Save nderkach/899281d7e6dd0d497533 to your computer and use it in GitHub Desktop.
Save nderkach/899281d7e6dd0d497533 to your computer and use it in GitHub Desktop.
couchsurfing.com API login and request test
#!/usr/bin/env python
import hmac
from hashlib import sha1
import requests
CS_URL = "https://hapi.couchsurfing.com"
PRIVATE_KEY = "v3#!R3v44y3ZsJykkb$E@CG#XreXeGCh"
# this is my fake couchsurfing.com account, feel free to play with it :)
LOGIN_PAYLOAD = '{"actionType":"manual_login","credentials":{"authToken":"qwerty","email":"nzoakhvi@sharklasers.com"}}'
def get_url_signature(key, msg):
return hmac.new(key.encode("utf-8"), msg.encode("utf-8"), sha1).hexdigest()
if __name__ == "__main__":
PATH = "/api/v2/sessions"
signature = get_url_signature(PRIVATE_KEY, PATH+LOGIN_PAYLOAD)
print(signature)
headers = {
"Accept": "application/json",
"X-CS-Url-Signature": signature,
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en;q=1",
"Content-Type": "application/json; charset=utf-8",
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 5.0.1; Android SDK built for x86 Build/LSX66B) Couchsurfing/android/20141121013910661/Couchsurfing/3.0.1/ee6a1da"
}
resp = requests.post(CS_URL + PATH, headers=headers, data=LOGIN_PAYLOAD)
print(resp.status_code)
print(resp.content)
session_user = resp.json()["sessionUser"]
user_id = session_user["id"]
access_token = session_user["accessToken"]
PATH = "/api/v2/users/" + user_id
signature = get_url_signature("{0}.{1}".format(PRIVATE_KEY, user_id), PATH)
print(signature)
headers.update({
"X-CS-Url-Signature": signature,
"X-Access-Token": access_token
})
resp = requests.get(CS_URL + PATH, headers=headers)
print(resp.status_code)
print(resp.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment