Skip to content

Commit 0b32932

Browse files
committed
first commit
0 parents  commit 0b32932

File tree

9 files changed

+237
-0
lines changed

9 files changed

+237
-0
lines changed

LICENSE.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (C) 2018 joeyism
2+
3+
Licensed under the Apache License, Version 2.0 (the 'License');
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an 'AS IS' BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# oauth2_google_login
2+
Gets OAuth2 access token from Google/YouTube automatically, using [requests_oauthlib](https://github.com/requests/requests-oauthlib)
3+
4+
## Installation
5+
6+
```bash
7+
pip3 install --user oauth2_google_login
8+
```
9+
10+
## Usage
11+
12+
### With Default Chrome Webdriver
13+
First, setup Chrome Webdriver so that it is in PATH, which can be done in terminal
14+
15+
```bash
16+
export PATH=$PATH:/home/username/Downloads/chromedriver
17+
```
18+
19+
Then in Python, run
20+
21+
```python
22+
from oauth2_google_login import get_access_token
23+
24+
auth = get_access_token(
25+
email = "user@email.com",
26+
password = "password",
27+
client_id="1234567",
28+
client_secret="a1b2c3d4e5",
29+
scope = ['https://www.googleapis.com/auth/yt-analytics.readonly', "https://www.googleapis.com/auth/youtube.readonly"]
30+
)
31+
32+
auth.access_token # Facebook access token
33+
```
34+
35+
### With Custom Webdriver
36+
37+
```python
38+
from oauth2_google_login import get_access_token
39+
from selenium import webdriver
40+
41+
driver = webdriver.Chrome("/home/username/Downloads/chromedriver")
42+
43+
auth = get_access_token(
44+
email = "user@email.com",
45+
password = "password",
46+
client_id="1234567",
47+
client_secret="a1b2c3d4e5",
48+
scope = ['https://www.googleapis.com/auth/yt-analytics.readonly', "https://www.googleapis.com/auth/youtube.readonly"],
49+
driver = driver
50+
)
51+
52+
auth.access_token # Facebook access token
53+
```
54+
55+
## Versions
56+
57+
**1.0.x**
58+
* First Publish

README.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
oauth2_google_login
2+
===================
3+
4+
Gets OAuth2 access token from Google/YouTube automatically, using
5+
`requests_oauthlib <https://github.com/requests/requests-oauthlib>`__
6+
7+
Installation
8+
------------
9+
10+
.. code:: bash
11+
12+
pip3 install --user oauth2_google_login
13+
14+
Usage
15+
-----
16+
17+
With Default Chrome Webdriver
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
First, setup Chrome Webdriver so that it is in PATH, which can be done
21+
in terminal
22+
23+
.. code:: bash
24+
25+
export PATH=$PATH:/home/username/Downloads/chromedriver
26+
27+
Then in Python, run
28+
29+
.. code:: python
30+
31+
from oauth2_google_login import get_access_token
32+
33+
auth = get_access_token(
34+
email = "user@email.com",
35+
password = "password",
36+
client_id="1234567",
37+
client_secret="a1b2c3d4e5",
38+
scope = ['https://www.googleapis.com/auth/yt-analytics.readonly', "https://www.googleapis.com/auth/youtube.readonly"]
39+
)
40+
41+
auth.access_token # Facebook access token
42+
43+
With Custom Webdriver
44+
~~~~~~~~~~~~~~~~~~~~~
45+
46+
.. code:: python
47+
48+
from oauth2_google_login import get_access_token
49+
from selenium import webdriver
50+
51+
driver = webdriver.Chrome("/home/username/Downloads/chromedriver")
52+
53+
auth = get_access_token(
54+
email = "user@email.com",
55+
password = "password",
56+
client_id="1234567",
57+
client_secret="a1b2c3d4e5",
58+
scope = ['https://www.googleapis.com/auth/yt-analytics.readonly', "https://www.googleapis.com/auth/youtube.readonly"],
59+
driver = driver
60+
)
61+
62+
auth.access_token # Facebook access token
63+
64+
Versions
65+
--------
66+
67+
**1.0.x** \* First Publish

oauth2_google_login/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from os.path import dirname, basename, isfile
2+
from .functions import get_access_token
3+
4+
__version__ = "1.0.0"
5+
6+
import glob
7+
modules = glob.glob(dirname(__file__)+"/*.py")
8+
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
584 Bytes
Binary file not shown.
2.17 KB
Binary file not shown.

oauth2_google_login/functions.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from selenium import webdriver
2+
from selenium.webdriver.support.ui import WebDriverWait
3+
from selenium.webdriver.support import expected_conditions as EC
4+
from selenium.webdriver.common.by import By
5+
from os import listdir
6+
from os.path import isfile, join
7+
from requests_oauthlib import OAuth2Session
8+
import json
9+
10+
11+
def __get_client_secret_file__(filename):
12+
json_data = open(filename)
13+
d = json.load(json_data)["web"]
14+
json_data.close()
15+
return d
16+
17+
18+
def get_access_token(username, password, client_id = "", client_secret = "", scope= [], driver = None):
19+
20+
if driver == None:
21+
driver = webdriver.Chrome()
22+
23+
auth_uri = "https://accounts.google.com/o/oauth2/auth",
24+
token_uri = "https://accounts.google.com/o/oauth2/token"
25+
authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
26+
redirect_uri = 'https://localhost:8080/'
27+
28+
google = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scope)
29+
30+
authorization_url, state = google.authorization_url(authorization_base_url, access_type="offline", prompt="select_account")
31+
32+
driver.get(authorization_url)
33+
34+
username_elem = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, 'identifierId')))
35+
username_elem.send_keys(username)
36+
driver.find_element_by_id("identifierNext").click()
37+
38+
password_elem = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.NAME, 'password')))
39+
password_elem.send_keys(password)
40+
driver.find_element_by_id("passwordNext").click()
41+
42+
if len(WebDriverWait(driver, 3).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="view_container"]/form/div[2]/div/div/div/ul/li[1]/div')))) != 0:
43+
driver.find_element_by_xpath('//*[@id="view_container"]/form/div[2]/div/div/div/ul/li[1]/div').click()
44+
45+
46+
next_btn = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, 'submit_approve_access')))
47+
next_btn.click()
48+
49+
wait = WebDriverWait(driver, 10)
50+
wait.until(lambda driver: redirect_uri in driver.current_url)
51+
52+
redirect_response = driver.current_url
53+
54+
google.fetch_token(token_uri, client_secret=client_secret, authorization_response=redirect_response)
55+
56+
driver.close()
57+
58+
return google

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.rst

setup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from setuptools import setup, find_packages
2+
# To use a consistent encoding
3+
from codecs import open
4+
from os import path
5+
import re
6+
7+
here = path.abspath(path.dirname(__file__))
8+
9+
version = re.search(
10+
'^__version__\s*=\s*"(.*)"',
11+
open('oauth2_google_login/__init__.py').read(),
12+
re.M
13+
).group(1)
14+
15+
# Get the long description from the README file
16+
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
17+
long_description = f.read()
18+
19+
setup(
20+
name = 'oauth2_google_login',
21+
packages = ['oauth2_google_login'], # this must be the same as the name above
22+
version = version,
23+
description = 'Returns Google/YouTube OAuth2 access token',
24+
author = 'Joey Sham',
25+
author_email = 'sham.joey@gmail.com',
26+
url = 'https://github.com/joeyism/oauth2_google_login', # use the URL to the github repo
27+
download_url = 'https://github.com/joeyism/oauth2_google_login/dist/' + version + '.tar.gz',
28+
keywords = ['oauth2', 'google', 'login', 'youtube'],
29+
classifiers = [],
30+
install_requires=["selenium", "requests_oauthlib"],
31+
)

0 commit comments

Comments
 (0)