2

What do I want to achieve?

Upload Photos From Given Dir to Google Photos Album

Reference Links Guide I am using

Files

Google.py: it Creates Service Object as per Need

main.py: I have imported Service obj from Google.py

Google.py

import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.auth.transport.requests import Request

def Create_Service(client_secret_file, api_name, api_version, *scopes):
    print(client_secret_file, api_name, api_version, scopes, sep='-')
    CLIENT_SECRET_FILE = client_secret_file
    API_SERVICE_NAME = api_name
    API_VERSION = api_version
    SCOPES = [scope for scope in scopes[0]]

    cred = None

    pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
    # print(pickle_file)

    if os.path.exists(pickle_file):
        with open(pickle_file, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
            cred = flow.run_local_server()

        with open(pickle_file, 'wb') as token:
            pickle.dump(cred, token)


    service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
    print(API_SERVICE_NAME, 'service created successfully')
    return service
     
   #Error 1 will be there if include try block
   # except Exception as e:
   # print(e)
   # return None

main.py

import os
from Google import Create_Service

API_NAME = 'photoslibrary'
API_VERSION = 'v1'
CLIENT_SECRET_FILE = 'client.json'
SCOPES = ['https://www.googleapis.com/auth/photoslibrary',
          'https://www.googleapis.com/auth/photoslibrary.sharing']

service = Create_Service(CLIENT_SECRET_FILE,API_NAME, API_VERSION, SCOPES)          
repons = service.albums().list().execute()

Problem

In main.py File Service, var should contain album and other methods. but it not there?

However, I have completed authentication without Error.

No problem in generating a new token file.

Error 1

  Traceback (most recent call last):
  File "/home/owais/Fiverr/GooglePhotos/photos/main.py", line 11, in <module>
    repons = service.albums().list().execute()
AttributeError: 'NoneType' object has no attribute 'albums'

Error 2

   return wrapped(*args, **kwargs)
  File "/home/owais/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 300, in build
    static_discovery=static_discovery,
  File "/home/owais/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 405, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion("name: %s  version: %s" % (serviceName, version))
googleapiclient.errors.UnknownApiNameOrVersion: name: photoslibrary  version: v1

What I Did.

Made new File For Wrote new Code but the error is there.

Moreover made a new project Too!

It looks like Google is blocking Me!

But there is not error or waring in google console.

  • In your script, `repons = service.albums().list().execute()` is used. But in your error message, `repons = service.album().list().execute()` is used. Can I ask you about your current script? By the way, when `Google.py` is merged with `main.py` as one file, what result will you obtain? – Tanaike May 12 '21 at 01:14
  • error message is also ```repons = service.albums().list().execute()``` . i updated it. and if i merge files it produce same error. – Qureshi Owais May 12 '21 at 05:21
  • Thank you for replying. Unfortunately, I cannot replicate your situation. When I tested your script, unfortunately, no error occurs. This is due to my poor skill. I deeply apologize for this. When I could correctly replicate your situation, I would like to think of the solution. – Tanaike May 12 '21 at 06:43
  • Found Ans merge it please [stack Q&A](https://stackoverflow.com/questions/66689941/google-photos-api-new-version) – Qureshi Owais May 13 '21 at 17:12
  • Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer? By this, I think that it will be useful for other users who have the same issue. – Tanaike May 14 '21 at 01:00

1 Answers1

1

Add the parameter static_discovery=False

service = build(API_SERVICE_NAME, API_VERSION, credentials=cred,static_discovery=False)

Google Photos API - new version?