wechat-oauth2 documentation · pdf filethe process is similar to that of oauth 1.0 but...

21
WeChat-OAuth2 Documentation Release 0.1.2 codeif Apr 21, 2017

Upload: lamcong

Post on 28-Mar-2018

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 DocumentationRelease 0.1.2

codeif

Apr 21, 2017

Page 2: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,
Page 3: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

Contents

1 Installation 3

2 Usage 5

3 API Reference 73.1 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Upgrade Notes 134.1 WeChat OAuth2 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Python Module Index 15

i

Page 4: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

ii

Page 5: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

A simple Python WeChat OAuth2 library built on top of rauth.

Contents 1

Page 6: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

2 Contents

Page 7: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

CHAPTER 1

Installation

Install the module with one of the following commands:

$ pip install WeChat-OAuth2

Or if you must:

$ easy_install Wechat-OAuth2

3

Page 8: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

4 Chapter 1. Installation

Page 9: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

CHAPTER 2

Usage

If you want to check out the complete API documentation, go ahead.

The easiest way to get started is by setting up a service wrapper. To do so simply import the service container object:

from wechat_oauth2 import WeChatService

wechat = WeChatService(appid='app id',secret='app secret')

Using the service wrapper API we can obtain an access token after the authorization URL has been visited by theclient. First generate the authorization URL:

redirect_uri = 'https://example.com/connect/login_success.html'params = {'scope': 'snsapi_userinfo',

'redirect_uri': redirect_uri}

url = wechat.get_authorize_url(**params)

Once this URL has been visited and (presumably) the client authorizes the application an access token can be obtained:

# the code should be returned upon the redirect from the authorize step,# be sure to use it here (hint: it's in the URL!)session = wechat.get_auth_session(code)

print session.get('userinfo').json()['openid']

5

Page 10: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

6 Chapter 2. Usage

Page 11: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

CHAPTER 3

API Reference

Information regarding the consumer API.

API

The API is exposed via service wrappers, which provide convenient WeChat OAuth2 flow methods as well as sessionmanagement.

Each service type has specialized Session objects, which may be used directly.

WeChat Service

class wechat_oauth2.WeChatService(appid, secret, name=’wechat’, ac-cess_token_url=’https://api.weixin.qq.com/sns/oauth2/access_token’,authorize_url=’https://open.weixin.qq.com/connect/oauth2/authorize’,base_url=’https://api.weixin.qq.com/sns/’, session_obj=None)

OAuth2.0

This class wraps an Ofly service i.e., Shutterfly. The process is similar to that of OAuth 1.0 but simplified.

You might intialize WeChatService something like this:

service = WeChatService(appid='123',secret='456')

A signed authorize URL is then produced via calling service.get_authorize_url. Once this has been visited bythe client and assuming the client authorizes the request.

Normal API calls can now be made using a session instance. Retrieve the authenticated session like so:

session = service.get_auth_session(code)

# now we can make regular Requests' callsr = session.get('userinfo')

7

Page 12: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

Parameters

• appid – ID

• secret –

• name – The service name, defaults to wechat.

• authorize_url – Authorize endpoint, defaults to ‘‘.

• base_url – A base URL from which to construct requests, defaults to None.

• session_obj (rauth.Session) – Object used to construct sessions with, defaults towechat_oauth2.WeChatSession

get_access_token(code, method=’GET’, **kwargs)

:

{"access_token":"ACCESS_TOKEN","expires_in":7200,"refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE","unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"

}

access_tokenexpires_in access_tokenrefresh_token access_tokenopenidscope ,unionid userinfo

:

{"errcode":40029,"errmsg":"invalid code"}

Parameters

• code – code

• method (str) – A string representation of the HTTP method to be used, defaults to GET.

• **kwargs (dict) – Optional arguments. Same as Requests.

get_auth_session(code, method=’GET’, **kwargs)ets an access token, intializes a new authenticated session with the access token. Returns an instance ofsession_obj.

get_authorize_url(redirect_uri=None, scope=’snsapi_base’, **params)Returns a formatted authorize URL.

get_session(access_token=None, openid=None)If provided, the access_token and openid parameter is used to initialize an authenticated session, otherwisean unauthenticated session object is generated. Returns an instance of session_obj..

Parameters

8 Chapter 3. API Reference

Page 13: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

• access_token – access_token with which to initilize the session.

• openid – openid with which to initilize the session.

WeChat Session

class wechat_oauth2.WeChatSession(appid=None, secret=None, access_token=None, openid=None,service=None, access_token_key=None)

close()Closes all adapters and as such the session

delete(url, **kwargs)Sends a DELETE request. Returns Response object.

Parameters

• url – URL for the new Request object.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

get(url, **kwargs)Sends a GET request. Returns Response object.

Parameters

• url – URL for the new Request object.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

get_adapter(url)Returns the appropriate connection adapter for the given URL.

Return type requests.adapters.BaseAdapter

head(url, **kwargs)Sends a HEAD request. Returns Response object.

Parameters

• url – URL for the new Request object.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

merge_environment_settings(url, proxies, stream, verify, cert)Check the environment and merge it with some settings.

Return type dict

mount(prefix, adapter)Registers a connection adapter to a prefix.

Adapters are sorted in descending order by key length.

options(url, **kwargs)Sends a OPTIONS request. Returns Response object.

Parameters

• url – URL for the new Request object.

3.1. API 9

Page 14: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

• **kwargs – Optional arguments that request takes.

Return type requests.Response

patch(url, data=None, **kwargs)Sends a PATCH request. Returns Response object.

Parameters

• url – URL for the new Request object.

• data – (optional) Dictionary, bytes, or file-like object to send in the body of theRequest.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

post(url, data=None, json=None, **kwargs)Sends a POST request. Returns Response object.

Parameters

• url – URL for the new Request object.

• data – (optional) Dictionary, bytes, or file-like object to send in the body of theRequest.

• json – (optional) json to send in the body of the Request.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

prepare_request(request)Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settingsmerged from the Request instance and those of the Session.

Parameters request – Request instance to prepare with this session’s settings.

Return type requests.PreparedRequest

put(url, data=None, **kwargs)Sends a PUT request. Returns Response object.

Parameters

• url – URL for the new Request object.

• data – (optional) Dictionary, bytes, or file-like object to send in the body of theRequest.

• **kwargs – Optional arguments that request takes.

Return type requests.Response

rebuild_auth(prepared_request, response)When being redirected we may want to strip authentication from the request to avoid leaking credentials.This method intelligently removes and reapplies authentication where possible to avoid credential loss.

rebuild_method(prepared_request, response)When being redirected we may want to change the method of the request based on certain specs or browserbehavior.

rebuild_proxies(prepared_request, proxies)This method re-evaluates the proxy configuration by considering the environment variables. If we are

10 Chapter 3. API Reference

Page 15: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missingproxy keys for this URL (in case they were stripped by a previous redirect).

This method also replaces the Proxy-Authorization header where necessary.

Return type dict

request(method, url, **req_kwargs)A loose wrapper around Requests’ Session which injects OAuth 2.0 parameters. :param method: Astring representation of the HTTP method to be used. :type method: str :param url: The resource to berequested. :type url: str :param **req_kwargs: Keyworded args to be passed down to Requests. :type**req_kwargs: dict

resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, prox-ies=None, **adapter_kwargs)

Receives a Response. Returns a generator of Responses.

send(request, **kwargs)Send a given PreparedRequest.

Return type requests.Response

3.1. API 11

Page 16: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

12 Chapter 3. API Reference

Page 17: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

CHAPTER 4

Upgrade Notes

Notes for upgrading from release to release, if applicable.

WeChat OAuth2 Changelog

This provides a list of changes to WeChat-OAuth2 by release.

Changes in Version 0.0.1

• First commit.

13

Page 18: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

14 Chapter 4. Upgrade Notes

Page 19: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

Python Module Index

wwechat_oauth2, 7

15

Page 20: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

WeChat-OAuth2 Documentation, Release 0.1.2

16 Python Module Index

Page 21: WeChat-OAuth2 Documentation · PDF fileThe process is similar to that of OAuth 1.0 but simplified. You might intialize WeChatServicesomething like this: ... WeChat-OAuth2 Documentation,

Index

Cclose() (wechat_oauth2.WeChatSession method), 9

Ddelete() (wechat_oauth2.WeChatSession method), 9

Gget() (wechat_oauth2.WeChatSession method), 9get_access_token() (wechat_oauth2.WeChatService

method), 8get_adapter() (wechat_oauth2.WeChatSession method), 9get_auth_session() (wechat_oauth2.WeChatService

method), 8get_authorize_url() (wechat_oauth2.WeChatService

method), 8get_session() (wechat_oauth2.WeChatService method), 8

Hhead() (wechat_oauth2.WeChatSession method), 9

Mmerge_environment_settings()

(wechat_oauth2.WeChatSession method),9

mount() (wechat_oauth2.WeChatSession method), 9

Ooptions() (wechat_oauth2.WeChatSession method), 9

Ppatch() (wechat_oauth2.WeChatSession method), 10post() (wechat_oauth2.WeChatSession method), 10prepare_request() (wechat_oauth2.WeChatSession

method), 10put() (wechat_oauth2.WeChatSession method), 10

Rrebuild_auth() (wechat_oauth2.WeChatSession method),

10

rebuild_method() (wechat_oauth2.WeChatSessionmethod), 10

rebuild_proxies() (wechat_oauth2.WeChatSessionmethod), 10

request() (wechat_oauth2.WeChatSession method), 11resolve_redirects() (wechat_oauth2.WeChatSession

method), 11

Ssend() (wechat_oauth2.WeChatSession method), 11

Wwechat_oauth2 (module), 7WeChatService (class in wechat_oauth2), 7WeChatSession (class in wechat_oauth2), 9

17