前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python http 认证 auth

python http 认证 auth

作者头像
py3study
发布2020-01-06 11:14:50
1.1K0
发布2020-01-06 11:14:50
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
# -*- 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=('5********', '**********'))
    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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档