Skip to content

Instantly share code, notes, and snippets.

@harishletsgo
Created October 21, 2016 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harishletsgo/e76d170ef8353fac7004db2eb9815374 to your computer and use it in GitHub Desktop.
Save harishletsgo/e76d170ef8353fac7004db2eb9815374 to your computer and use it in GitHub Desktop.
Yelp APIv3 Fusion - oAuth 2.0 implementation
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
@harishletsgo
Copy link
Author

Can use a better way to access the token string from returned unicode JSON. Suggestions welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment