python request with authentication (access_token)

后端 未结 5 2106
别跟我提以往
别跟我提以往 2020-11-28 06:00

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

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

g

5条回答
  •  天命终不由人
    2020-11-28 06:29

    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'})
    

    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'})
    

提交回复
热议问题