问题
I have been working with the protected authentication of the netflix api and the python oauth2 library. I have no problem making signed requests, however, to allow users to sign in using their netflix accounts, I am running into a few problems when I try to get the access_token, I know there cases in which OAuth doesn't return a verifier
, even if its supposed to, however after being redirected from the authorization page of netflix I get something like this: http://127.0.0.1:5000/authorized_user?oauth_token=some_token&oauth_verifier=
with the verifier empty.
I am new to the library and quite didn't understand what to do when the verfier
is not present. Since, I successfully redirect the user to the netflix sign in/authorization page. I assume my error comes from this step which I don't fully understand. Below is a simplified (shell) version of what I am attempting. I would appreciate a push in the right direction, I read netflix documentation and read the library documentation but couldn't figure out what to do.
# Get request token (temporary)
resp, content = client.request(REQUEST_TOKEN_URL, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(parse_qsl(content))
print 'Request token'
print ' --> oauth_token = %s' % request_token['oauth_token']
print ' --> oauth_token_secret = %s' % request_token['oauth_token_secret']
print ' --> login_url = %s' % request_token['login_url']
# Redirect to netflix for user authorization
print 'Go to the following link: '
login_url = request_token['login_url']
access_token_url = '%s&oauth_consumer_key=%s' % (login_url, CONSUMER_KEY)
accepted = 'n'
while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ')
resp, content = client.request(access_token_url, "POST")
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, "POST")
access_token = dict(parse_qsl(content))
print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
回答1:
So it sounds like you're attempting to use python-oauth2. Unfortunately this library is widely considered abandoned-ware. I would highly recommend using a maintained library. For that I can recommend rauth. I'm the maintainer of rauth, for what it's worth.
Now unfortunately Netflix is not accepting new applications to their OAuth infrastructure. However I did write up an example for you that could try if you're willing to give rauth a shot. I can't promise it won't work without some tweaks, but here it is:
from rauth import OAuth1Service
import re
import webbrowser
request_token_url = 'http://api-public.netflix.com/oauth/request_token'
access_token_url = 'http://api-public.netflix.com/oauth/access_token'
authorize_url = 'https://api-user.netflix.com/oauth/login'
base_url = 'http://api-public.netflix.com/'
netflix = OAuth1Service(consumer_key='123',
consumer_secret='456',
request_token_url=request_token_url,
authorize_url=authorize_url,
access_token_url=access_token_url,
base_url=base_url)
request_token, request_token_secret = netflix.get_request_token()
oauth_callback = 'http://example.com/oauth/authorized'
params = {'oauth_callback': oauth_callback, 'application_name': 'your_app'}
authed_url = netflix.get_authorize_url(request_token, **params)
print 'Visit this URL in your browser: ' + authed_url
webbrowser.open(authed_url)
url_with_token = raw_input('Copy URL from your browser\'s address bar: ')
request_token = re.search('\?oauth_token=([^&]*)', url_with_token).group(1)
s = netflix.get_auth_session(request_token, request_token_secret)
r = s.get('users/current')
print r.content
A couple of things to note here: Netflix makes no mention of the verifier in their documentation. So I'm guessing that's why you see none. Secondly they are returning an "authorized" request token in place. Basically this token replaces the verifier pin in their flow.
Hope this helps!
来源:https://stackoverflow.com/questions/15756287/oauth2-library-and-netflix-api-return-none-access-token