Praw:API登录因客户端错误而失败

时间:2014-04-03 09:19:24

标签: python google-app-engine http-status-code-403 reddit praw

我无法使用下面的代码登录我自己的Reddit帐户。

错误讯息:

raise HTTPError(http_error_msg, response=self) HTTPError: 403 Client Error: Forbidden

有没有办法解决这个错误?

class PrawTest(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Yo, imma redit bot!')

    get_login= ConfigParser.ConfigParser()
    get_login.read("logins.ini")

    r = praw.Reddit(user_agent='Captain Reddit!')

    r.login(get_login.get("login1", "username"),get_login.get("login1","password"))


app = webapp2.WSGIApplication([('/hype_shit_up', PrawTest)], debug=True)

1 个答案:

答案 0 :(得分:0)

使用PRAW登录时,您的代码非常不必要地过于复杂。在the documentation中推荐的方式是让reddit = praw.Reddit('SOME-NAME-HERE')登录。然后,在praw.ini文件中,将其设置为如下所示:

[DEFAULT]
# A boolean to indicate whether or not to check for package updates.
check_for_updates=True

# Object to kind mappings
comment_kind=t1
message_kind=t4
redditor_kind=t2
submission_kind=t3
subreddit_kind=t5

# The URL prefix for OAuth-related requests.
oauth_url=https://oauth.reddit.com

# The URL prefix for regular requests.
reddit_url=https://www.reddit.com

# The URL prefix for short URLs.
short_url=https://redd.it

[SOME-NAME-HERE]
user_agent=USER-AGENT-HERE
username=REDDIT-ACCOUNT-USERNAME
password=REDDIT-ACCOUNT-PASSWORD
client_id=REDDIT-APP-CLIENT-ID
client_secret=REDDIT-APP-CLIENT-SECRET

找到用户代理的要求here

将客户端的用户代理字符串更改为唯一且具有描述性的内容,包括目标平台,唯一应用程序标识符,版本字符串和用户名作为联系信息,格式如下: <platform>:<app ID>:<version string> (by /u/<reddit username>)

- Example: User-Agent: android:com.example.myredditapp:v1.2.3 (by /u/kemitche)

  • 许多默认的用户代理(例如&#34; Python / urllib&#34;或&#34; Java&#34;)受到严格限制,以鼓励使用唯一的描述性用户代理字符串。
  • 包含版本号并在您构建应用程序时对其进行更新,这样我们就可以安全地阻止您的应用程序的旧错误/损坏版本。
  • 从不对您的用户代理撒谎。这包括欺骗流行的浏览器和欺骗其他机器人。我们将禁止极端偏见的骗子。

如果您有任何未来的问题,请不要犹豫,将其作为对此答案的评论!