Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,32 @@ user => {...}

If you want to see more complete examples, click [here](../master/examples)

#### Authorization
------------------
Get access_token using buffer [docs](https://bufferapp.com/developers/api/oauth)

```python

service = AuthService(client_id, client_secret, redirect_uri)

url = service.authorize_url

# Access the url and retrieve the token
auth_code = #Paste the code from the redirected url

access_token = service.get_access_token(auth_code)

api = service.create_session(access_token)
```

#### User
----------
A user represents a single Buffer user account.

```python

api = API(client_id='client_id',
client_secret='client_secret',
api = API(client_id='client_id',
client_secret='client_secret',
access_token='access_token')

# instantiate an user object
Expand Down
46 changes: 40 additions & 6 deletions buffpy/api.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import json
import urllib

from rauth import OAuth2Session
from rauth import OAuth2Session, OAuth2Service

from buffpy.response import ResponseObject


BASE_URL = 'https://api.bufferapp.com/1/%s'
PATHS = {
'INFO': 'info/configuration.json'
'INFO': 'info/configuration.json'
}

AUTHORIZE_URL = 'https://bufferapp.com/oauth2/authorize'
ACCESS_TOKEN = 'https://api.bufferapp.com/1/oauth2/token.json'


class API(object):
'''
Small and clean class that embrace all basic
operations with the buffer app
'''

def __init__(self, client_id, client_secret, access_token=None):
self.session = OAuth2Session( client_id=client_id,
client_secret=client_secret,
access_token=access_token)
self.session = OAuth2Session(client_id=client_id,
client_secret=client_secret,
access_token=access_token)

@property
def access_token(self):
Expand Down Expand Up @@ -46,7 +52,7 @@ def post(self, url, parser=None, **params):
if not self.session.access_token:
raise ValueError('Please set an access token first!')

headers = {'Content-Type':'application/x-www-form-urlencoded'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}

response = self.session.post(url=BASE_URL % url, headers=headers, **params)

Expand All @@ -66,3 +72,31 @@ def info(self):

response = self.get(url=PATHS['INFO'])
return ResponseObject(response)


class AuthService(object):

def __init__(self, client_id, client_secret, redirect_uri):
self.outh_service = OAuth2Service(client_id=client_id,
client_secret=client_secret,
name='buffer',
authorize_url=AUTHORIZE_URL,
access_token_url=ACCESS_TOKEN,
base_url=BASE_URL % '')

self.redirect_uri = redirect_uri

def create_session(self, access_token=None):
return self.outh_service.get_session(access_token)

def get_access_token(self, auth_code):
auth_code = urllib.unquote(auth_code).decode('utf8')
data = {'code': auth_code,
'grant_type': 'authorization_code',
'redirect_uri': self.redirect_uri}

return self.outh_service.get_access_token(data=data, decoder=json.loads)

@property
def authorize_url(self):
return self.outh_service.get_authorize_url(response_type='code', redirect_uri=self.redirect_uri)
Empty file added examples/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions examples/get_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from colorama import Fore

from buffpy import AuthService

client_id = 'add_apps_client_id'
client_secret = 'add_apps_secret'

print Fore.YELLOW + '---- START TOKEN RETRIEVING OPERATION ----' + Fore.RESET
redirect_uri = 'add_your_redirect_uri(with http)'


service = AuthService(client_id, client_secret, redirect_uri)

url = service.authorize_url
print Fore.GREEN + 'Access this url and retrieve the token: ' + Fore.RESET + url

auth_code = raw_input(Fore.GREEN + 'Paste the code from the redirected url: ' + Fore.RESET)
access_token = service.get_access_token(auth_code)
print Fore.GREEN + 'Acess TOKEN: ' + Fore.RESET + access_token

api = service.create_session(access_token)

#Do stuff with your session
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
]

setup(name='buffpy',
version='1.06',
version='1.07',
platforms='any',
description='Python library for Buffer App',
author='Vlad Temian',
Expand Down