-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LuiJason
authored and
LuiJason
committed
Aug 9, 2017
1 parent
207a99a
commit 7f1fceb
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |