带认证的python请求(access_token) [英] python request with authentication (access_token)

查看:943
本文介绍了带认证的python请求(access_token)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个API查询到python。命令行

I am trying to get an API query into python. The command line

curl --header "Authorization:access_token myToken" https://website.com/id

提供一些json输出。 myToken是一个十六进制变量,始终保持不变。我想从python这个调用,使我可以循环通过不同的id和分析输出。有任何想法吗?在需要认证之前,我使用urllib2。我还看了一下请求模块,但不知道如何做到这一点。

gives some json output. myToken is a hexadecimal variable that remains constant throughout. I would like to make this call from python so that I can loop through different ids and analyze the output. Any ideas? Before authentication was needed I have done that with urllib2. I have also taken a look at the requests module but couldn't figure out how to do that.

非常感谢。

推荐答案

requests 包对于HTTP请求有一个非常好的API,添加一个自定义头像这样(来源:官方文档

The requests package has a very nice API for HTTP requests, adding a custom header works like this (source: official docs:

>>> import requests
>>> response = requests.get(
... 'https://website.com/id', headers={'Authorization': 'access_token myToken'})

要使用外部依赖,使用标准库的urllib2的同样的事情看起来像这样(来源:缺少的手册):

If you don't want to use an external dependency, the same thing using urllib2 of the standard library looks like this (source: the missing manual):

>>> import urllib2
>>> response = urllib2.urlopen(
... urllib2.Request('https://website.com/id', headers={'Authorization': 'access_token myToken'})

这篇关于带认证的python请求(access_token)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆