Google Api Client - AttributeError: 'str' object has no attribute 'authorize'
Google Api Client - AttributeError: 'str' object has no attribute 'authorize'
我的代码一直在运行,直到我决定将我的 Google Api 凭据移动到我的 Docker 环境中。我正在使用 Flask 作为 Web 服务器框架。
这是我的设置:
DOCKER:
docker-撰写-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]
现在我收到以下错误:
AttributeError: 'str' object has no attribute 'authorize'
怎么了,我错过了什么?
在 docker
环境('string')中设置的 project_xxxxx.json
路径必须传递到 service_account.Credentials.from_service_account_file()
:
为清楚起见,更改 config.py:
中的变量名称
PATH_TO_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
然后,将 service_account
导入到您的 video.py 模块中,如下所示:
from google.oauth2 import service_account
和:
GET_CREDENTIALS = DevelopmentConfig.PATH_TO_CREDENTIALS
PASS_CREDENTIALS = service_account.Credentials.from_service_account_file(GET_CREDENTIALS)
最后,传递凭据,如下所示:
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=PASS_CREDENTIALS,
developerKey=None)
这会起作用。
我的代码一直在运行,直到我决定将我的 Google Api 凭据移动到我的 Docker 环境中。我正在使用 Flask 作为 Web 服务器框架。
这是我的设置:
DOCKER:
docker-撰写-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]
现在我收到以下错误:
AttributeError: 'str' object has no attribute 'authorize'
怎么了,我错过了什么?
在 docker
环境('string')中设置的 project_xxxxx.json
路径必须传递到 service_account.Credentials.from_service_account_file()
:
为清楚起见,更改 config.py:
中的变量名称PATH_TO_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
然后,将 service_account
导入到您的 video.py 模块中,如下所示:
from google.oauth2 import service_account
和:
GET_CREDENTIALS = DevelopmentConfig.PATH_TO_CREDENTIALS
PASS_CREDENTIALS = service_account.Credentials.from_service_account_file(GET_CREDENTIALS)
最后,传递凭据,如下所示:
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=PASS_CREDENTIALS,
developerKey=None)
这会起作用。