簡體   English   中英

Google 相冊 API 服務 obj NoneType' object 沒有屬性“相冊”

[英]Google Photos API service obj NoneType' object has no attribute 'album'

我想達到什么目標?

將給定目錄中的照片上傳到 Google 相冊

我正在使用的參考鏈接指南

文件

Google.py:它根據需要創建服務 Object

main.py:我從 Google.py 導入了 Service obj

谷歌.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

主文件

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()

問題

在 main.py 文件服務中,var 應該包含專輯和其他方法。 但它不在那里?

但是,我已經完成了沒有錯誤的身份驗證。

生成新的令牌文件沒有問題。

錯誤 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'

錯誤 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

我做了什么。

為編寫新代碼制作了新文件,但錯誤在那里。

而且做了一個新項目 Too!

看起來 Google 正在阻止我!

但是谷歌控制台中沒有錯誤或警告。

添加參數static_discovery=False

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

Google 相冊 API - 新版本?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM