我试图得到我的youtube频道的视频列表,并更新它,因为我请,我已经做了它与.net &这是相当容易的,但在python这是一种令人不安的每次我运行脚本,我得到的消息“请访问此URL授权此应用程序”虽然我使用相同的credentials.json文件,我用它在. net.
注:没有必要标记这个问题的重复或任何东西,因为我已经检查了许多相同的问题在这里的堆栈像this,this和this其他论坛太多,但似乎没有提供一个明确的具体解决方案,到目前为止,我已经尝试了每个解决方案都有相同的结果。
下面是我使用的示例代码:
任何帮助都将不胜感激。
import os
import sys
file_path = os.path.dirname(__file__)
module_path = os.path.join(file_path, "lib")
sys.path.append(module_path)
import pickle
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = [
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.upload",
"https://www.googleapis.com/auth/youtubepartner",
"https://www.googleapis.com/auth/youtube.force-ssl",
]
client_secrets_file = "credentials.json"
api_service_name = "youtube"
api_version = "v3"
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
youtube = get_authenticated_service()
request = youtube.channels().list(
part="contentDetails",
mine=True
)
response = request.execute()
print(response)
def get_authenticated_service():
if os.path.exists("CREDENTIALS_PICKLE_FILE"):
with open("CREDENTIALS_PICKLE_FILE", 'rb') as f:
credentials = pickle.load(f)
else:
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console()
with open("CREDENTIALS_PICKLE_FILE", 'wb') as f:
pickle.dump(credentials, f)
return googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
if __name__ == "__main__":
main()
1条答案
按热度按时间a1o7rhls1#
我很幸运地找到了这个问题的解决方案,这里有一个工作脚本示例,它将列出您在您的YouTube频道中的所有视频,并将要求授权代码只一次。
我知道这有点乱不过没关系