2

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 側からのコードの問題があるのか​​ を提案してください。

4

1 に答える 1