Skip to content

Instantly share code, notes, and snippets.

@simonmikkelsen
Created June 9, 2017 06:30
Show Gist options
  • Save simonmikkelsen/015b8cf7ddfa20e1a3970b9421809719 to your computer and use it in GitHub Desktop.
Save simonmikkelsen/015b8cf7ddfa20e1a3970b9421809719 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape
import sys
import json
import urllib
import httplib
try:
import requests
except ImportError:
print("requests not found - please run: pip install requests")
sys.exit()
def application (environ, start_response):
#Mapillary oauth test
client_id = 'Y0NtM3R4Zm52cTBOSUlrTFAwWFFFQTpmZjMxNmViM2JiYWZjMjJi'
client_secret = 'M ... ='
redirect_uri = 'http://localhost:7788/browser/api/index4.py'
# Gyllen: Try this:
# https://www.mapillary.com/connect?client_id=NlRpbEg1aVRrbGNmcGhCZGhHSFpGdzo0YTBhYTIwZjk3Mzg5NGJh&scope=user:read+public:upload+public:write+private:read+private:write+private:upload&response_type=token&redirect_uri=http://localhost:3087
url = "https://www.mapillary.com/connect?client_id="+client_id+"&scope=user:read+public:upload+public:write+private:read+private:write+private:upload&response_type=token&redirect_uri="+redirect_uri
if len(environ['QUERY_STRING']) == 0:
# No query string: Step 1: Redirect the user to Mapillary to get permission.
response_headers = [
('Location', url)
]
status = '302 Moved Temporary'
start_response(status, response_headers)
response_body = ''
return [response_body]
# Step 2: Send the access token to Mapillary to get an authorization token:
# Returns a dictionary in which the values are lists
query_params = parse_qs(environ['QUERY_STRING'])
access_token = query_params['access_token'][0]
payload = {'client_id': client_id, 'client_secret': client_secret, 'redirect_uri':redirect_uri, 'grant_type':'authorization_code', 'code':access_token}
data = urllib.urlencode(payload)
h = httplib.HTTPSConnection('a.mapillary.com')
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
h.request('POST', '/v2/oauth/token', data, headers)
r = h.getresponse()
resp = r.read()+"\n"+data
status = '200 OK'
# Use this response_body when it works.
#response_body = environ['QUERY_STRING']
response_body = str(resp)
response_headers = [
('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))
]
start_response(status, response_headers)
return [response_body]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment