我已经在这个问题上工作了一段时间,
我正在使用youtube API列出频道中的所有播放列表项,我在下面阅读了此内容:Youtube Data API - How to avoid Google OAuth redirect URL authorization
但是我每次运行时仍需要打开浏览器并验证程序以访问自己的youtube帐户。
根据此页面:https://developers.google.com/youtube/v3/getting-started
4. If your application will use any API methods that require user authorization,
read the authentication guide to learn how to implement OAuth 2.0 authorization.
我只需要列出频道中所有可用的视频。
google提供的代码示例:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from google_auth_oauthlib.flow import Flow
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "CREDFILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.playlistItems().list(
part="snippet",
maxResults=30,
playlistId="PLAYLISTID"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
我已将credential.json文件作为指定的代码,但是我仍然需要对自己进行身份验证,然后将身份验证代码粘贴到命令行中。我对这里提供credential.json文件的使用感到困惑。
似乎是credentials = flow.run_console()
引起的,是否可以使用其他方法来获取证书?
Oauth2的工作方式是,需要用户同意才能授予对用户数据的访问权限。尽管浏览器窗口请求用户授予您的应用程序访问权限,但该许可已被授予。
您总是必须至少同意一次,YouTube API无法解决此问题。
您正在查看的另一个问题(Youtube Data API - How to avoid Google OAuth redirect URL authorization )仅说明,一旦刷新令牌存储一次,就无需再次加载它。 Google api Java客户端库可以做到这一点,因为该库中内置了用户凭据存储。
您必须在python客户端库中编写类似的代码,但我认为不支持。