Я новичок в использовании любого API, а также HTTP-запросов, поэтому у меня проблемы. Я не уверен, как передать информацию о токене в API после того, как получу ее из запроса GET. API imgur говорит, что ему нужны три конечные точки: http://api.imgur.com/auth, но я дойдите до второго, потому что я не могу передать запрошенные токены.
Документация по модулям для меня ужасно расплывчата: https://github.com/maraujop/requests-oauth < / а>
Вот написанный мной код, который должен успешно пройти аутентификацию, но возвращает html-страницу http://pastebin.com/19hnBy1C.
import requests
from oauth_hook import OAuthHook
import cgi
OAuthHook.consumer_key = 'XXXXXXX'
OAuthHook.consumer_secret = 'YYYYY'
#just in case
oauth_hook = OAuthHook(header_auth=True)
#request the tokens, using the keys set above
r = requests.get(url='https://api.imgur.com/oauth/request_token', hooks={'pre_request': oauth_hook,})
#parse the lsit
tokenList = cgi.parse_qs(r.content)
token = tokenList['oauth_token']
tokenSecret = tokenList['oauth_token_secret']
#this is where I'm not sure what to do,
#I create a new hook with the tokens I received
oauth_hook = OAuthHook(access_token=token[0], access_token_secret=tokenSecret[0])
#send the GET request
r = requests.get(url='https://api.imgur.com/oauth/authorize', hooks={'pre_request': oauth_hook,})
#this is that HTML that requires me to enter account info, how do I do that in python?
print r.content
#this is the next step, which, if you uncomment the last print, shows that the auth failed.
r = requests.get(url='https://api.imgur.com/oauth/access_token', hooks={'pre_request': oauth_hook,})
#print r.text
Как лучше всего продолжить?
Я подумал, что, может быть, я смогу отправить POST на authorize
api с моим именем пользователя / паролем в качестве данных или параметра, но это не сработало.
API Imgur предлагает мне посмотреть некоторые твиттер-документы, чтобы получить хорошее представление, но тот, который я читаю: http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/, немного над моей головой, так как это PHP, хотя мне кажется, что это то, что я должен делать.