Skip to main content

OAuth of Fanfou

Project description

https://img.shields.io/travis/akgnah/fanfou-py/master.svg https://img.shields.io/pypi/v/fanfou.svg https://img.shields.io/pypi/l/fanfou.svg https://img.shields.io/badge/code_style-pep8-orange.svg

Installation

$ sudo pip install fanfou

Usage

>>> import fanfou

Step 1: Authorize

The module provides several ways to authorize, see more details on Fanfou API OAuth.

Way 1:

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.OAuth(consumer)    # (1)(2)
>>> request_token = client.request_token()
>>> print(client.authorize_url)    # browse the url to authorize
>>> access_token = client.access_token()    # done

(1). The default callback is ‘http://localhost:8080’.

(2). The default authorize_url is ‘m.fanfou.com/…’, you can pass auth_host=’fanfou.com’ to change it.

Maybe you handling the callback elsewhere, create a new client and get access_token, like that:

>>> client = fanfou.OAuth(consumer, request_token)
>>> access_token = client.access_token()    # done
>>> # or
>>> client = fanfou.OAuth(consumer)
>>> access_token = client.access_token(request_token)    # done

Way 2:

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.OAuth(consumer, callback='oob')
>>> request_token = client.request_token()
>>> print(client.authorize_url)    # browse the url and copy verifier_code
>>> verifier_code = 'your verifier_code'
>>> access_token = client.access_token(oauth_verifier=verifier_code)    # done

You can also create a new client and get access_token, like the way 1 above.

Way 3:

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> access_token =  {'key': 'your key', 'secret': 'your secret'}    # access_token is what you saved before
>>> client = fanfou.OAuth(consumer, access_token)    # done

Way 4:

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.XAuth(consumer, 'username', 'password')    # done
>>> access_token = client.access_token()    # optional, if you want to save access_token

Using https:

In the lastest edition, you can using https like that:

>>> consumer = {'key': 'your key', 'secret': 'your secret'}
>>> client = fanfou.XAuth(consumer, 'username', 'password', fake_https=True)

The fake_https is available in all authorize ways.

Step 2: Access API

We assume that you’ve got client on Step 1, now you have two styles to access API.

Style 1:

>>> import json
>>>
>>> resp = client.request('/statuses/home_timeline', 'GET')  # resp is a HTTPResponse instance
>>> print(resp.code)
>>> data = json.loads(resp.read())    # Python 3: data = json.loads(resp.read().decode('utf8'))
>>> for item in data:
>>>     print(item['text'])
>>>
>>> body = {'status': 'update status test 1'}
>>> resp = client.request('/statuses/update', 'POST', body)
>>> print(resp.code)

Style 2:

>>> import json
>>>
>>> fanfou.bound(client)    # note the line
>>>
>>> body = {'page': 2, 'count': 20, 'mode': 'lite'}
>>> resp = client.statuses.home_timeline()
>>> data = json.loads(resp.read())    # Python 3: data = json.loads(resp.read().decode('utf8'))
>>> for item in data:
>>>     print(item['text'])
>>>
>>> body = {'status': 'update status test 2'}
>>> resp = client.statuses.update(body)
>>> print(resp.code)

If you want to use style 2, you must fanfou.bound(client) before use. They have the same effect, just two different styles.

Just put all you want to request args to a dict (above is body), and then access a API. If you want to upload a photo, please see pack_image. More API details on Fanfou API Apicategory.

What’s new in 0.2.x

>>> fanfou.bound(client)
>>>
>>> resp = client.users.show()
>>> data = resp.json()    # equal: data = json.loads(resp.read().decode('utf8'))

In this update, you can get a Python object directly by using resp.json().

More details

pack_image(args, binary=None)

On /account/update_profile_image and /photos/upload you need to upload a image, pack_image can help you work easily.

>>> # update profile image
>>> args = {'image': 'test.jpg', 'mode': 'lite'}
>>> body, headers = fanfou.pack_image(args)
>>> resp = client.account.update_profile_image(body, headers)
>>> # or, resp = client.request('/account/update_profile_image', 'POST', body, headers)
>>> print(resp.code)
>>>
>>> # upload photo
>>> args = {'photo': 'http://static.fanfou.com/img/fanfou.png', 'status': 'upload online photo'}
>>> body, headers = fanfou.pack_image(args)
>>> resp = client.photos.upload(body, headers)
>>> print(resp.code)

Just put the filename in the args, then pack_image it, and then you can access API. The image file can be local or network file, pack_image will auto read it.

Sometimes you want to provide binary bytes instead of filename when you’re writing a webapp, because the data you get from the form is binary. (like m.setq.me)

>>> f = open('test.jpg')
>>> args = {'photo': 'test.jpg', 'status': 'upload local photo'}
>>> body, headers = fanfou.pack_image(args, binary=f.read())  # note the line
>>> f.close()
>>> resp = client.photos.upload(body, headers)
>>> print(resp.code)

auth classes

The __init__ method for auth classes is as follows:

class OAuth (oauth_consumer, oauth_token=None, callback=None, auth_host=None, https=False, fake_https=False)

class XAuth (oauth_consumer, username, password, https=False, fake_https=False)

Thanks

Thank Fanfou and thank you for tolerating my poor English.

If you have any questions, I am here @home2.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fanfou-0.2.6.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

fanfou-0.2.6-py2.py3-none-any.whl (10.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fanfou-0.2.6.tar.gz.

File metadata

  • Download URL: fanfou-0.2.6.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fanfou-0.2.6.tar.gz
Algorithm Hash digest
SHA256 6e79ccaf6c4d283bfb6234f9f748f36f862339ec7208726966f5bca5a643a0e1
MD5 f83dcbe8c062efa84935903b5fb35b84
BLAKE2b-256 4d1f34bd89d34d324129986f8ce42bf8bbddba8b0cd4249b213f9dcfb5656446

See more details on using hashes here.

File details

Details for the file fanfou-0.2.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fanfou-0.2.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 035bb28d919b20c190562583cb5b89f81f7f9cca6873b6ff55e83d9f83805890
MD5 a95ae509fa69b53b9afef81258901cb0
BLAKE2b-256 eafb91f1aa616596aecc96fd871aadf643ebbedba6eed402acd0a4d7a031b8de

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page