Twitter API USERS / Search在Android App中返回Forbidden

时间:2014-03-01 19:17:20

标签: android twitter

我的MainActivity.java有这些声明

final static String CONSUMER_KEY = "MY-CONSUMER-KEY";
final static String CONSUMER_SECRET = "MY-CONSUMER-SECRET";
final static String TwitterTokenURL = "https://api.twitter.com/oauth2/token";
final static String TwitterStreamURL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";
final static String TwitterUsersURL = "https://api.twitter.com/1.1/users/search.json?q=";

我在异步任务中使用以下代码来搜索用户

String urlApiKey = URLEncoder.encode(MainActivity.CONSUMER_KEY, "UTF-8");
String urlApiSecret = URLEncoder.encode(MainActivity.CONSUMER_SECRET, "UTF-8");
String combined = urlApiKey + ":" + urlApiSecret;
String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP);
HttpPost httpPost = new HttpPost(MainActivity.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);

if (auth != null && auth.token_type.equals("bearer")) {

    // Step 3: Authenticate API requests with bearer token
    HttpGet httpGet = new HttpGet(MainActivity.TwitterUsersURL + screenName);

    // 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");
    // update the results with the body of the response
    results = getResponseBody(httpGet);
}

当我在TwitterStreamURL上发送请求时,我得到了正确的回复。但是在TwitterUsersURL上的任何请求,我都会被“禁止”。

我一直在网上浏览,当我们使用http代替https时,每个人都遇到了这个问题。

我做错了什么?

编辑:我正在使用仅应用程序身份验证

0 个答案:

没有答案