python http 认证 auth oauth 认证

# -*- coding: utf-8 -*-
import requests

BASE_URL = ‘https://api.github.com’

def construct_url(end_point):
return ‘/’.join([BASE_URL, end_point])

def basic_auth():
“””http基本认证”””
response = requests.get(construct_url(‘user’), auth=(‘584807419’, ‘zk199245ZK’))
print response.text
print response.request.headers

basic_auth()

def basic_oauth():
headers = {‘Authorization’: ‘token fafsffsfsfafasfasfasfsafafasf’}
response = requests.get(construct_url(‘user’), headers=headers)
print response.request.headers
print response.text
print response.status_code

from requests.auth import AuthBase

class GithubAuth(AuthBase):
def __init__(self, token):
self.token = token

def __call__(self, r):
#requests 加 headers
r.headers[‘Authorization’] = ”.join([‘token’, self.token])
return r

def oauth_advanced():
auth = GithubAuth(‘fafsffsfsfafasfasfasfsafafasf’)
response = requests.get(construct_url(‘user’), auth=auth)
print response.text

oauth_advanced()

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!