【问题标题】:Why am I getting a 429 error with the Reddit API?为什么我在使用 Reddit API 时收到 429 错误?
【发布时间】:2020-04-19 23:53:13
【问题描述】:

我一直在尝试使用烧瓶和 Reddit api,但无论我尝试什么,我似乎每次尝试接收访问令牌时都会遇到 429 'too many requests' 错误.

初始用户身份验证工作没有任何问题,我收到返回的代码并可以验证我已经在我的 Reddit 设置中对应用程序进行了身份验证。我还确保使用独特的用户代理,因为这似乎解决了大多数人的问题,并且没有使用任何诸如“bot”、“curl”、“web”之类的词或其他任何可能的词可能是拦截器。

据我所知,由于请求过多而导致速率受限,我也很容易接受。

鉴于这是我第一次同时使用烧瓶和 Reddit API,我确定我遗漏了一些明显的东西,但是 4 小时后,大量谷歌搜索和阅读所有文档,我没有明白我在这里做错了什么。

import requests
import requests.auth
from flask import Flask, redirect, request, url_for
import string
import random

app = Flask(__name__)

client_id = "client id of my web app"
client_secret = "client secret of my web app"

base_auth_url = 'https://www.reddit.com/api/v1'

authorization_endpoint = '/authorize'
access_token_endpoint = '/access_token'


@app.route("/login")
def get_the_auth_code():
    state = state_generator()
    params = {
        'client_id': client_id,
        'response_type': 'code',
        'state': state,
        'redirect_uri': 'http://localhost:5000/redditor',
        'scope': 'identity',
        'user-agent': 'myapp v0.1 by /u/myredditusername'
    }
    return redirect(url_builder(authorization_endpoint, params))


@app.route("/redditor")
def get_the_access_token():
    code = request.args.get('code')
    client_auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
    post_data = {
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': 'http://localhost:5000/redditor',
        'user-agent': 'myapp v0.1 by /u/myredditusername'
    }
    response = requests.post(base_auth_url + access_token_endpoint, auth=client_auth, data=post_data)
    token_json = response.json()
    return token_json


def state_generator(size=25, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


def url_builder(endpoint, parameters):
    params = '&'.join(['%s=%s' % (k, v) for k, v in parameters.items()])
    url = '%s%s?%s' % (base_auth_url, endpoint, params)
    return url


if __name__ == "__main__":
    app.run(debug=True)

【问题讨论】:

    标签: python-3.x api flask localhost reddit


    【解决方案1】:

    在 Reddit 社区(特别是 /u/nmtake)的帮助下,我发现了哪里出错了。

    在我的get_the_access_token() 函数中,我将用户代理字段添加到我的数据参数中,而不是将其声明为标题的一部分。

    代替:

    post_data = {
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': 'http://localhost:5000/redditor',
        'user-agent': 'myapp v0.1 by /u/myredditusername'
    }
    response = requests.post(base_auth_url + access_token_endpoint, auth=client_auth, data=post_data)
    

    我现在正在使用以下,效果很好:

    post_data = {
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': 'http://localhost:5000/redditor',
    }
    post_headers = {
        'user-agent': 'myapp v0.1 by /u/myredditusername'
    }
    response = requests.post(base_auth_url + access_token_endpoint, auth=client_auth, data=post_data, headers=post_headers)
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 2012-02-16
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多