From 7f1fcebd7fead1cc651118d80467ba4ee9ef506e Mon Sep 17 00:00:00 2001 From: LuiJason Date: Wed, 9 Aug 2017 23:15:03 +0800 Subject: [PATCH] add authen.py in ch5 --- ch5/authen.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ch5/authen.py diff --git a/ch5/authen.py b/ch5/authen.py new file mode 100644 index 0000000..5afb7bf --- /dev/null +++ b/ch5/authen.py @@ -0,0 +1,44 @@ +# -*- 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(): + """ + 基本认证 + """ + response = requests.get(construct_url('user'),auth=('imoocdemo','imoocdemo123')) + print(response.text) + print(response.request.headers) + +def basic_oauth(): + headers = {'Authorization': 'token dd6322fa6c57a548268453dc245cbcdc352a7811'} + # user/emails + response = requests.get(construct_url('user/emails'),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('dd6322fa6c57a548268453dc245cbcdc352a7811') + response = requests.get(construct_url('user/emails'),auth=auth) + print(response.text) +# basic_auth() +# basic_oauth() +oauth_advanced()