gpt4 book ai didi

android - Reddit 在 android 中的集成

转载 作者:搜寻专家 更新时间:2023-11-01 08:45:46 26 4
gpt4 key购买 nike

我使用它的 API 实现了 reddit 集成。下面是我登录 reddit 的代码:

private void LoginMethod(){

String jsonString = "";
DefaultHttpClient httpclient = new DefaultHttpClient();

final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(3);
fields.add(new BasicNameValuePair("user", "test"));//will ask for a user to enter the password later
fields.add(new BasicNameValuePair("passwd", "test11"));
fields.add(new BasicNameValuePair("api_type", "json"));

final HttpPost request = new HttpPost("https://ssl.reddit.com/api/login");

try {
request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8));

HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();

jsonString = EntityUtils.toString(entity);

System.out.println("response from redit = " + jsonString);

JSONObject jObject = new JSONObject(jsonString);
modhash = jObject.getJSONObject("json").getJSONObject("data").getString("modhash");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

}

使用上面的方法,它只是有时有效并且大部分得到如下响应:

02-10 16:36:57.008: I/System.out(26970): <!doctype html>
02-10 16:36:57.008: I/System.out(26970): <html>
02-10 16:36:57.008: I/System.out(26970): <head>
02-10 16:36:57.008: I/System.out(26970): <title>Too Many Requests</title>
02-10 16:36:57.008: I/System.out(26970): <style>
02-10 16:36:57.008: I/System.out(26970): body {
02-10 16:36:57.008: I/System.out(26970): font: small verdana, arial, helvetica, sans-serif;
02-10 16:36:57.008: I/System.out(26970): width: 600px;
02-10 16:36:57.008: I/System.out(26970): margin: 0 auto;
02-10 16:36:57.008: I/System.out(26970): }
02-10 16:36:57.008: I/System.out(26970): h1 {
02-10 16:36:57.008: I/System.out(26970): height: 40px;
02-10 16:36:57.008: I/System.out(26970): background: transparent url(//www.redditstatic.com/reddit.com.header.png) no-repeat scroll top right;
02-10 16:36:57.008: I/System.out(26970): }
02-10 16:36:57.008: I/System.out(26970): </style>
02-10 16:36:57.008: I/System.out(26970): </head>
02-10 16:36:57.008: I/System.out(26970): <body>
02-10 16:36:57.008: I/System.out(26970): <h1>whoa there, pardner!</h1>
02-10 16:36:57.008: I/System.out(26970):
02-10 16:36:57.008: I/System.out(26970): <p>we're sorry, but you appear to be a bot and we've seen too many requests
02-10 16:36:57.008: I/System.out(26970): from you lately. we enforce a hard speed limit on requests that appear to come
02-10 16:36:57.008: I/System.out(26970): from bots to prevent abuse.</p>
02-10 16:36:57.008: I/System.out(26970): <p>if you are not a bot but are spoofing one via your browser's user agent
02-10 16:36:57.008: I/System.out(26970): string: please change your user agent string to avoid seeing this message
02-10 16:36:57.008: I/System.out(26970): again.</p>
02-10 16:36:57.008: I/System.out(26970): <p>please wait 3 second(s) and try again.</p>
02-10 16:36:57.008: I/System.out(26970): <p>as a reminder to developers, we recommend that clients make no
02-10 16:36:57.008: I/System.out(26970): more than <a href="http://github.com/reddit/reddit/wiki/API">one
02-10 16:36:57.008: I/System.out(26970): request every two seconds</a> to avoid seeing this message.</p>
02-10 16:36:57.008: I/System.out(26970): </body>
02-10 16:36:57.008: I/System.out(26970): </html>

我不知道如何处理这类问题。我已经对此进行了研发,但是没有太多的细节或示例可用于 reddit 集成。

所以,帮忙解决这个bug。

最佳答案

除了 Antonio MG 的回答之外,您还应该根据 reddit API rules 设置自定义用户代理。 :

"Many default User-Agents (like "Python/urllib" or "Java") are drastically limited to encourage unique and descriptive user-agent strings."

至少,它应该包括 reddit 工作人员的联系信息,以便在必要时取得联系。根据要求,它应该是以下形式:

<platform>:<app ID>:<version string> (by /u/<reddit username>)

例如

android:com.example.myredditapp:v1.2.3 (by /u/kemitche)

最后,你应该强烈考虑使用 OAuth 2 访问 reddit 的 API,而不是直接登录; "cookie" authentication is scheduled for deprecation .

关于android - Reddit 在 android 中的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28430273/

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