本文主要是介绍python http 认证 auth oauth 认证,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# -*- coding: utf-8 -*-
import requestsBASE_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=('5********', '**********'))print response.textprint response.request.headersbasic_auth()def basic_oauth():headers = {'Authorization': 'token fafsffsfsfafasfasfasfsafafasf'}response = requests.get(construct_url('user'), headers=headers)print response.request.headersprint response.textprint response.status_codefrom requests.auth import AuthBaseclass GithubAuth(AuthBase):def __init__(self, token):self.token = tokendef __call__(self, r):#requests 加 headersr.headers['Authorization'] = ''.join(['token', self.token])return rdef oauth_advanced():auth = GithubAuth('fafsffsfsfafasfasfasfsafafasf')response = requests.get(construct_url('user'), auth=auth)print response.textoauth_advanced()
这篇关于python http 认证 auth oauth 认证的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!