1
|
|
|
""" |
2
|
|
|
Plugin object. |
3
|
|
|
|
4
|
|
|
.. moduleauthor:: Jaisen Mathai <[email protected]> |
5
|
|
|
""" |
6
|
|
|
from __future__ import print_function |
7
|
|
|
from builtins import object |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class GooglePhotos(object): |
11
|
|
|
"""A class to execute plugin actions.""" |
12
|
|
|
pass |
13
|
|
|
|
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
import json |
17
|
|
|
|
18
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow |
19
|
|
|
from google.auth.transport.requests import AuthorizedSession |
20
|
|
|
from google.oauth2.credentials import Credentials |
21
|
|
|
|
22
|
|
|
scopes = [ |
23
|
|
|
"https://www.googleapis.com/auth/photoslibrary", |
24
|
|
|
"https://www.googleapis.com/auth/photoslibrary.appendonly", |
25
|
|
|
"https://www.googleapis.com/auth/photoslibrary.sharing" |
26
|
|
|
] |
27
|
|
|
|
28
|
|
|
auth_file = 'client_id.json' |
29
|
|
|
|
30
|
|
|
try: |
31
|
|
|
creds = Credentials.from_authorized_user_file(auth_file, scopes) |
32
|
|
|
except: |
33
|
|
|
print('no creds') |
34
|
|
|
#flow = InstalledAppFlow.from_client_secrets_file('/Users/jaisen/Downloads/client_secret_1004259275591-5g51kj0feetbet88o8le5i16hbr3ucb6.apps.googleusercontent.com-3.json', scopes) |
35
|
|
|
flow = InstalledAppFlow.from_client_secrets_file('/Users/jaisen/Downloads/client_secret_1004259275591-ogsk179e96cs0h126qj590mofk86gdqo.apps.googleusercontent.com.json', scopes) |
36
|
|
|
creds = flow.run_local_server() |
37
|
|
|
cred_dict = { |
38
|
|
|
'token': creds.token, |
39
|
|
|
'refresh_token': creds.refresh_token, |
40
|
|
|
'id_token': creds.id_token, |
41
|
|
|
'scopes': creds.scopes, |
42
|
|
|
'token_uri': creds.token_uri, |
43
|
|
|
'client_id': creds.client_id, |
44
|
|
|
'client_secret': creds.client_secret |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
with open(auth_file, 'w') as f: |
48
|
|
|
f.write(json.dumps(cred_dict)) |
49
|
|
|
|
50
|
|
|
session = AuthorizedSession(creds) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
session.headers["Content-type"] = "application/octet-stream" |
55
|
|
|
session.headers["X-Goog-Upload-Protocol"] = "raw" |
56
|
|
|
session.headers["X-Goog-Upload-File-Name"] = 'foo.jpg' #os.path.basename(photo_file_name) |
57
|
|
|
|
58
|
|
|
photo_file = open("/Users/jaisen/Downloads/test.png", mode='rb') |
59
|
|
|
photo_bytes = photo_file.read() |
60
|
|
|
|
61
|
|
|
upload_token = session.post('https://photoslibrary.googleapis.com/v1/uploads', photo_bytes) |
62
|
|
|
if (upload_token.status_code == 200) and (upload_token.content): |
63
|
|
|
create_body = json.dumps({"newMediaItems":[{"description":"","simpleMediaItem":{"uploadToken":upload_token.content.decode()}}]}, indent=4) |
64
|
|
|
resp = session.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', create_body).json() |
65
|
|
|
print(resp) |
66
|
|
|
if "newMediaItemResults" in resp: |
67
|
|
|
status = resp["newMediaItemResults"][0]["status"] |
68
|
|
|
print(status)""" |
69
|
|
|
|