Created
June 12, 2017 06:52
-
-
Save simonmikkelsen/5feb1aac7d622e60a3f7c67b90a2c80b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = 'MDRkNTM5NTVlMTAwYjg1ZTVmNmVmZjBlODU2Y2I2NWE=' | |
redirect_uri = 'http://localhost:3087' | |
# 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=NlRpbEg1aVRrbGNmcGhCZGhHSFpGdzo0YTBhYTIwZjk3Mzg5NGJh&scope=user:read+public:upload+public:write+private:read+private:write+private:upload&response_type=token&redirect_uri=http://localhost:3087" | |
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) | |
# Now content type is text/html | |
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