gpt4 book ai didi

python - 如何使用 praw 检查登录凭据是否有效

转载 作者:行者123 更新时间:2023-12-04 09:46:09 26 4
gpt4 key购买 nike

所以我试图制作一个从 subreddit 中获取随机帖子 url 的机器人,并希望它检查登录凭据是否有效,所以我想出了以下内容:

import praw
import pandas as pd
import datetime as dt

username = input("Your user name:\n")
password = input("Your Password:\n")



reddit = praw.Reddit(client_id='xxxxxxxxxxxxx', \
client_secret='xxxxxxxxxxxxxxxxxxxxxxxx', \
user_agent='Fetch', \
username=username, \
password=password)

if reddit == thats where i dont know what to type
submission = reddit.subreddit("gtaonline").random()
print(submission.url)

else:
print("Enter valid credentials")
quit()

最佳答案

您可以使用 reddit.user.me() .此方法为您提供经过身份验证的用户,其副作用会导致 Reddit使用凭据的实例。

如果凭据有效,则返回 Redditor实例。如果您的凭据无效,调用它会导致 prawcore.ResponseException .

您可以使用此事实来测试您的凭据:

from prawcore import ResponseException

def authenticated(reddit):
"""Determine whether the given Reddit instance has valid credentials."""
try:
reddit.user.me()
except ResponseException:
return False
else:
return True

如果在脚本中定义此函数,则条件变为

if authenticated(reddit):

关于python - 如何使用 praw 检查登录凭据是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62102184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com