Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Replace Google authentication strategy #342

Merged
merged 4 commits into from
Apr 24, 2022
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pyyaml = ">=4.2b1"
pywin32 = {markers = "platform_system == 'Windows'",version = "*"}
psutil = "*"
versiongit = "*"
google-auth-oauthlib = "*"

[dev-packages]
pytest = ">=5.0.1"
Expand Down
36 changes: 17 additions & 19 deletions gphotos/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from urllib3.util.retry import Retry
from typing import List, Optional
from google_auth_oauthlib.flow import InstalledAppFlow

from json import load, dump, JSONDecodeError
import logging
Expand Down Expand Up @@ -40,6 +41,8 @@ def __init__(
self.token_file: Path = token_file
self.session = None
self.token = None
self.secrets_file = secrets_file

try:
with secrets_file.open("r") as stream:
all_json = load(stream)
Expand Down Expand Up @@ -84,29 +87,24 @@ def authorize(self):
token_updater=self.save_token,
)
else:
self.session = OAuth2Session(
self.client_id,
scope=self.scope,
redirect_uri=self.redirect_uri,
auto_refresh_url=self.token_uri,
auto_refresh_kwargs=self.extra,
token_updater=self.save_token,
flow = InstalledAppFlow.from_client_secrets_file(
self.secrets_file,
scopes=self.scope
)
flow.run_local_server()

# Redirect user to Google for authorization
authorization_url, _ = self.session.authorization_url(
authorization_base_url, access_type="offline", prompt="select_account"
)
print(f"Please go here and authorize, {authorization_url}\n",)
self.session = flow.authorized_session()

# Get the authorization verifier code from the callback url
response_code = input("Paste the response token here:\n")
# Mapping for backward compatibility
oauth2_token = {
"access_token" : flow.credentials.token,
"refresh_token": flow.credentials.refresh_token,
"token_type" : "Bearer",
"scope" : flow.credentials.scopes,
"expires_at": flow.credentials.expiry.timestamp()
}

# Fetch the access token
self.token = self.session.fetch_token(
self.token_uri, client_secret=self.client_secret, code=response_code
)
self.save_token(self.token)
self.save_token(oauth2_token)

# set up the retry bevaiour for the authorized session
retries = Retry(
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"requests_oauthlib",
"pyyaml>=4.2b1",
"psutil",
"google-auth-oauthlib"
]

develop_reqs = [
Expand Down