Google Api Client - AttributeError: 'str' object has no attribute 'authorize'

﹥>﹥吖頭↗ 提交于 2021-02-11 13:05:36

问题


My code was working up until I decided to move my Google Api credentials into my Docker env. I'm using Flask as a web server framework.

This is my set up:

DOCKER:

docker-compose-dev.yml

environment:
  - FLASK_ENV=development
  - APP_SETTINGS=project.config.DevelopmentConfig
  - GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/project/api/resources/youtube/urls/project-84a0ef4dcd33.json  

FLASK:

config.py

 class DevelopmentConfig(BaseConfig):
    CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"

video.py

from project.config import DevelopmentConfig

CREDENTIALS = DevelopmentConfig.CREDENTIALS
YOUTUBE_API_SERVICE_NAME = DevelopmentConfig.YOUTUBE_API_SERVICE_NAME
YOUTUBE_API_VERSION = DevelopmentConfig.YOUTUBE_API_VERSION

def youtube_id(track_name):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=CREDENTIALS,
    developerKey=None)

    search_response = youtube.search().list(
    q=track_name,
    part="id,snippet",
    ).execute()

    videos = []
    videos_ids = []
    channels = []
    playlists = []

    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                 search_result["id"]["videoId"]))
            videos_ids.append("%s" % (search_result["id"]["videoId"]))
        elif search_result["id"]["kind"] == "youtube#channel":
            channels.append("%s (%s)" % (search_result["snippet"]["title"],
                                   search_result["id"]["channelId"]))
        elif search_result["id"]["kind"] == "youtube#playlist":
            playlists.append("%s (%s)" % (search_result["snippet"]["title"],
                                    search_result["id"]["playlistId"]))

    return videos_ids[0]

Now I'm getting the following error:

AttributeError: 'str' object has no attribute 'authorize'

what is wrong, what am I missing?


回答1:


Path to project_xxxxx.json set in docker environment (the 'string') must be passed into service_account.Credentials.from_service_account_file():

for clarity sake, change variable name in config.py:

PATH_TO_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')

then, import service_account into your video.py module, like so:

from google.oauth2 import service_account

and:

GET_CREDENTIALS = DevelopmentConfig.PATH_TO_CREDENTIALS
PASS_CREDENTIALS = service_account.Credentials.from_service_account_file(GET_CREDENTIALS)

Finally, pass credentials, like so:

youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=PASS_CREDENTIALS,
    developerKey=None)

This will work.



来源:https://stackoverflow.com/questions/58291816/google-api-client-attributeerror-str-object-has-no-attribute-authorize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!