Skip to content

Commit

Permalink
Merge pull request #1 from 4admin2root/master
Browse files Browse the repository at this point in the history
add authen.py in ch5
  • Loading branch information
jian-en authored Dec 20, 2017
2 parents 207a99a + 7f1fceb commit bb9e480
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ch5/authen.py
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()

0 comments on commit bb9e480

Please sign in to comment.