Created
October 21, 2016 08:07
-
-
Save harishletsgo/e76d170ef8353fac7004db2eb9815374 to your computer and use it in GitHub Desktop.
Yelp APIv3 Fusion - oAuth 2.0 implementation
This file contains 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
import re | |
import requests | |
from oauthlib.oauth2 import BackendApplicationClient | |
from requests_oauthlib import OAuth2Session | |
#Fill app key details from Yelp | |
client_id = '<client id here>' | |
client_secret = '<client secret here>' | |
#Authenticate and get access token | |
client = BackendApplicationClient(client_id=client_id) | |
yelp = OAuth2Session(client=client) | |
token = yelp.fetch_token(token_url='https://api.yelp.com/oauth2/token', client_id=client_id, client_secret=client_secret) | |
#Construct custom Authentication header | |
#Can use a better way to access the token string from returned unicode JSON. This is quick and dirty | |
tokenstring = str(token) | |
accesstok = re.findall(r"'access_token': u'(.*?)', ",tokenstring) | |
authhead = "Bearer " + accesstok[0] | |
head = {'Authorization': authhead} | |
#Call needed yelp endpoint with custom header using requests module | |
r = requests.get("https://api.yelp.com/v3/businesses/north-india-restaurant-san-francisco/reviews", headers= head) | |
print r.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can use a better way to access the token string from returned unicode JSON. Suggestions welcome!