Twitter友谊/创建提供403 / Forbidden错误消息

时间:2016-10-18 08:02:59

标签: android twitter twitter-fabric twitter-follow

我正在使用twitter文档关注用户使用以下网址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;
    }

所以请说明网址有什么问题,或者android方面是否有任何代码问题。

2 个答案:

答案 0 :(得分:3)

来自official docs

  

如果您已经是该用户的朋友,则可能会返回HTTP 403,但出于性能原因,即使友谊已存在,您也可能收到200 OK消息。

这种行为可以正常;)

答案 1 :(得分:1)

我已经尝试了很多,最后得到了Twitter社区的回答。我希望这个答案对其他人有帮助。

问题是誓言https://dev.twitter.com/oauth,如果你想关注,那么你可以使用下面的代码重定向,

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=ScreenNameofUser"));

此链接将重定向到用户的Twitter关注页面。

相关问题