Google Api客户端-AttributeError:"str"对象没有属性"authorize" [英] Google Api Client - AttributeError: 'str' object has no attribute 'authorize'

查看:58
本文介绍了Google Api客户端-AttributeError:"str"对象没有属性"authorize"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码一直在工作,直到我决定将我的Google Api凭据移到我的Docker环境中.我正在使用Flask作为网络服务器框架.

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.

这是我的设置:

DOCKER:

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  

烧瓶:

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]

现在我遇到以下错误:

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

出什么问题了,我想念什么?

what is wrong, what am I missing?

推荐答案

docker 环境(字符串")中设置的 project_xxxxx.json 的路径必须传递到 service_account.Credentials.from_service_account_file():

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

为清楚起见,请在 config.py 中更改变量名称:

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

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

然后,将 service_account 导入到您的 video.py 模块中,如下所示:

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

from google.oauth2 import service_account

和:

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)

这将起作用.

这篇关于Google Api客户端-AttributeError:"str"对象没有属性"authorize"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆