gpt4 book ai didi

android - Twitter friendships/create 给出 403/Forbidden 错误信息

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

我正在使用 twitter 文档来关注使用以下 url https://dev.twitter.com/rest/reference/post/friendships/create 的用户

使用 API 时 https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true ,我收到 403 错误,

调用api的代码如下。

public String getTwitterFollow(String url) {

//?screen_name=MDforLives&follow=true

String results = null;
// Step 1: Encode consumer key and secret
try {
// URL encode the consumer key and secret
String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8");
String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8");

// Concatenate the encoded consumer key, a colon character, and the
// encoded consumer secret
String combined = urlApiKey + ":" + urlApiSecret;

// Base64 encode the string
String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP);

// Step 2: Obtain a bearer token
HttpPost httpPost = new HttpPost(TwitterTokenURL);
httpPost.setHeader("Authorization", "Basic " + base64Encoded);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
String rawAuthorization = getResponseBody(httpPost);
Authenticated auth = jsonToAuthenticated(rawAuthorization);

// Applications should verify that the value associated with the
// token_type key of the returned object is bearer
if (auth != null && auth.token_type.equals("bearer")) {

url = url.replaceAll(" ", "%20");

// Step 3: Authenticate API requests with bearer token
HttpPost httpGet = new HttpPost(url);

// construct a normal HTTPS request and include an Authorization
// header with the value of Bearer <>
httpGet.setHeader("Authorization", "Bearer " + auth.access_token);
// httpGet.setHeader("Content-Type", "application/json");

StringEntity stringEntity;

JSONObject obj=new JSONObject();

obj.put("screen_name","MDforLives");
obj.put("follow","true");

stringEntity = new StringEntity(obj.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
// httpGet.setEntity(stringEntity);

// update the results with the body of the response
results = getResponseBody(httpGet);

System.out.println("results returns" + results);

}
} catch (Exception ex) {
}
return results;
}

所以请建议 url 有什么问题,或者 android 端是否有任何代码问题。

最佳答案

来自 official docs :

If you are already friends with the user a HTTP 403 may be returned, though for performance reasons you may get a 200 OK message even if the friendship already exists.

这种行为可能是正常的;)

关于android - Twitter friendships/create 给出 403/Forbidden 错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40102768/

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