-
Notifications
You must be signed in to change notification settings - Fork 0
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
rohitkhatri
committed
Feb 17, 2019
1 parent
76ef559
commit 76ed132
Showing
10 changed files
with
276 additions
and
1 deletion.
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
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 @@ | ||
include requirements.txt |
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,48 @@ | ||
Metadata-Version: 1.1 | ||
Name: google-python-sdk | ||
Version: 1.0.0 | ||
Summary: Python Google API | ||
Home-page: https://github.com/rohitkhatri/google-python-sdk | ||
Author: Rohit Khatri | ||
Author-email: developer.rohitkhatri@gmail.com | ||
License: GPL | ||
Description: # google-python-sdk | ||
=================== | ||
#### Python - Google API | ||
|
||
**google-python-sdk** is a simple client for google api. | ||
|
||
## Installation | ||
``` | ||
sudo pip install google-python-sdk | ||
``` | ||
|
||
## Using | ||
```python | ||
from google import API | ||
api = API('CLIENT_ID', 'CLIENT_SECRET', 'API_KEY', 'ACCESS_TOKEN') | ||
``` | ||
|
||
## References https://www.googleapis.com/oauth2/v3/userinfo | ||
```python | ||
profile = api.get_profile() | ||
``` | ||
|
||
## References https://oauth2.googleapis.com/tokeninfo | ||
```python | ||
tokeninfo = api.get_token_info() | ||
``` | ||
|
||
|
||
## Contributing | ||
[https://github.com/rohitkhatri/google-python-sdk](https://github.com/rohitkhatri/google-python-sdk) | ||
|
||
|
||
Keywords: google data api python | ||
Platform: UNKNOWN | ||
Classifier: Development Status :: 4 - Beta | ||
Classifier: Intended Audience :: Developers | ||
Classifier: Operating System :: OS Independent | ||
Classifier: Programming Language :: Python :: 3.5 | ||
Classifier: Operating System :: OS Independent | ||
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
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 |
---|---|---|
@@ -1 +1,30 @@ | ||
# google-python-sdk | ||
google-python-sdk | ||
=================== | ||
#### Python - Google API | ||
|
||
**google-python-sdk** is a simple client for google api. | ||
|
||
## Installation | ||
``` | ||
sudo pip install google-python-sdk | ||
``` | ||
|
||
## Using | ||
```python | ||
from google import API | ||
api = API('CLIENT_ID', 'CLIENT_SECRET', 'API_KEY', 'ACCESS_TOKEN') | ||
``` | ||
|
||
## References https://www.googleapis.com/oauth2/v3/userinfo | ||
```python | ||
profile = api.get_profile() | ||
``` | ||
|
||
## References https://oauth2.googleapis.com/tokeninfo | ||
```python | ||
tokeninfo = api.get_token_info() | ||
``` | ||
|
||
|
||
## Contributing | ||
[https://github.com/rohitkhatri/google-python-sdk](https://github.com/rohitkhatri/google-python-sdk) |
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,7 @@ | ||
__version__ = '1.0.0' | ||
__author__ = 'Rohit Khatri' | ||
__license__ = 'MIT' | ||
|
||
|
||
from google.exceptions import * | ||
from google.api import API |
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,132 @@ | ||
import requests | ||
from urllib.parse import urlencode | ||
from google.exceptions import GoogleException | ||
|
||
|
||
class API: | ||
_client_id = None | ||
_client_secret = None | ||
_api_key = None | ||
_access_token = None | ||
_api_base_url = 'https://www.googleapis.com/google/v3' | ||
_auth_url = 'https://accounts.google.com/o/oauth2/auth' | ||
_profile_url = 'https://www.googleapis.com/oauth2/v3/userinfo' | ||
_exchange_code_url = 'https://accounts.google.com/o/oauth2/token' | ||
_scope = [ | ||
'https://www.googleapis.com/auth/userinfo.profile', | ||
'https://www.googleapis.com/auth/userinfo.email' | ||
] | ||
_part = 'id,snippet' | ||
|
||
def __init__(self, client_id, client_secret, api_key, access_token=None, api_url=None): | ||
self._client_id = client_id | ||
self._client_secret = client_secret | ||
self._api_key = api_key | ||
self._access_token = access_token | ||
|
||
if api_url: | ||
self.api_url = api_url | ||
|
||
def get(self, endpoint, **kwargs): | ||
if self._access_token: | ||
kwargs['access_token'] = self._access_token | ||
else: | ||
kwargs['api_key'] = self._api_key | ||
|
||
if 'part' not in kwargs: | ||
kwargs['part'] = self._part | ||
|
||
return self.response(self._get('{}{}'.format(self._api_base_url, endpoint), params=kwargs)) | ||
|
||
def post(self, endpoint, **kwargs): | ||
if self._access_token: | ||
kwargs['access_token'] = self._access_token | ||
else: | ||
kwargs['api_key'] = self._api_key | ||
|
||
return self.response(self._post('{}{}'.format(self._api_base_url, endpoint), params=kwargs)) | ||
|
||
def get_auth_url(self, redirect_uri, **kwargs): | ||
kwargs = {**{ | ||
'response_type': 'code', | ||
'redirect_uri': redirect_uri, | ||
'client_id': self._client_id, | ||
'access_type': 'offline', | ||
'approval_prompt': 'force' | ||
}, **kwargs} | ||
|
||
if 'scope' not in kwargs: | ||
kwargs['scope'] = self._scope | ||
|
||
kwargs['scope'] = ' '.join(kwargs['scope']) | ||
|
||
return '{}?{}'.format(self._auth_url, urlencode(kwargs)) | ||
|
||
def exchange_code(self, code, redirect_uri): | ||
params = { | ||
'code': code, 'client_id': self._client_id, 'client_secret': self._client_secret, | ||
'redirect_uri': redirect_uri, 'grant_type': 'authorization_code' | ||
} | ||
headers = {'Content-Type': 'application/x-www-form-urlencoded'} | ||
response = self.response(self._post(self._exchange_code_url, params=params, headers=headers)) | ||
|
||
if response and 'access_token' in response: | ||
self._access_token = response['access_token'] | ||
|
||
return response | ||
|
||
def refresh_token(self, refresh_token): | ||
params = { | ||
'client_id': self._client_id, 'client_secret': self._client_secret, 'refresh_token': refresh_token, | ||
'grant_type': 'refresh_token' | ||
} | ||
headers = {'Content-Type': 'application/x-www-form-urlencoded'} | ||
response = self.response(self._post(self._exchange_code_url, params=params, headers=headers)) | ||
|
||
if response and 'access_token' in response: | ||
self._access_token = response['access_token'] | ||
|
||
return response | ||
|
||
def get_token_info(self): | ||
return self.response(self._get('https://oauth2.googleapis.com/tokeninfo', { | ||
'access_token': self._access_token | ||
})) | ||
|
||
def get_profile(self): | ||
return self.response(self._get(self._profile_url, { | ||
'access_token': self._access_token | ||
})) | ||
|
||
@staticmethod | ||
def _get(url, params=None, **kwargs): | ||
result = requests.get(url, params=params, **kwargs) | ||
_response = result.json() | ||
|
||
if _response and 'error' in _response: | ||
raise GoogleException(_response['error']['code'], _response['error']['message'], result) | ||
|
||
if result.status_code != 200: | ||
raise GoogleException(result.status_code, result.reason, result) | ||
|
||
return result | ||
|
||
@staticmethod | ||
def _post(url, params=None, **kwargs): | ||
result = requests.post(url, data=params, **kwargs) | ||
_response = result.json() | ||
|
||
if _response and 'error' in _response: | ||
if isinstance(_response['error'], str): | ||
raise GoogleException(result.status_code, _response['error'], result) | ||
else: | ||
raise GoogleException(_response['error']['code'], _response['error']['message'], result) | ||
|
||
if result.status_code != 200: | ||
raise GoogleException(result.status_code, result.reason, result) | ||
|
||
return result | ||
|
||
@staticmethod | ||
def response(response): | ||
return response.json() |
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,13 @@ | ||
class GoogleException(Exception): | ||
def __init__(self, code, message, response): | ||
self.status_code = code | ||
self.error_type = message | ||
self.message = message | ||
self.response = response | ||
self.get_error_type() | ||
|
||
def get_error_type(self): | ||
json_response = self.response.json() | ||
|
||
if 'error' in json_response and 'errors' in json_response['error']: | ||
self.error_type = json_response['error']['errors'][0]['reason'] |
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 @@ | ||
requests==2.12.1 |
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,7 @@ | ||
[bdist_wheel] | ||
universal = 1 | ||
|
||
[egg_info] | ||
tag_build = | ||
tag_date = 0 | ||
|
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,31 @@ | ||
try: | ||
from setuptools import setup | ||
except ImportError: | ||
from distutils.core import setup | ||
|
||
try: | ||
long_description = open('README.md').read() | ||
except IOError: | ||
long_description = "" | ||
|
||
setup( | ||
name='google-python-sdk', | ||
version='1.0.0', | ||
description='Python Google API', | ||
long_description=long_description, | ||
url='https://github.com/rohitkhatri/google-python-sdk', | ||
author='Rohit Khatri', | ||
author_email='developer.rohitkhatri@gmail.com', | ||
license='GPL', | ||
classifiers=[ | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
"Operating System :: OS Independent", | ||
'Programming Language :: Python :: 3.5', | ||
"Operating System :: OS Independent", | ||
'Topic :: Software Development :: Libraries :: Python Modules' | ||
], | ||
keywords="google data api python", | ||
packages=['google'], | ||
install_requires=['requests'] | ||
) |