1

这是我第一次发布到 stackOverflow,我想使用 oauth2(仅应用程序身份验证)从 twitter 获取不记名令牌,并按照此处的说明进行操作,但我的代码返回错误 401 未授权

这是我的代码:

package com.example.twitterapp;

import java.net.URLEncoder;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import android.util.Base64;
import android.util.Log;

import com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest;

public class BearerTokenJsonRequest extends
        SpringAndroidSpiceRequest<BearerToken> {


    String API_STRING;



    @Override
    public BearerToken loadDataFromNetwork() throws Exception {
        try{
            API_STRING = URLEncoder.encode("xvz1evFS4wEEPTGEFPHBog","UTF-8")+  
                ":"+URLEncoder.encode("L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg","UTF-8");
            Log.d("JSON" , "apistring: "+ API_STRING);
            }catch(Exception e){
                e.printStackTrace();
            }

        String encodedString = Base64.encodeToString(API_STRING.getBytes("UTF-8"), Base64.DEFAULT);
                Log.d("ENCODED" , "Base64: "+ encodedString);       




        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + encodedString); 
        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 



        ResponseEntity<BearerToken> entity= getRestTemplate().exchange(
                "https://api.twitter.com/oauth2/token?grant_type=client_credentials", 
                HttpMethod.POST, new HttpEntity<Object>(headers), BearerToken.class);

        return entity.getBody();


    }

}

此代码返回错误 401 未经授权。我确保消费者密钥和秘密是正确的,尝试对 Base64 编码的字符串进行 url 编码

这是我的 logcat 错误:

09-20 13:21:23.190: W/RestTemplate(16646): POST request for
"https://api.twitter.com/oauth2/token?grant_type=client_credentials"
resulted in 401 (Unauthorized); invoking error handler 09-20
13:21:23.210: E//RequestRunner.java:134(16646): 13:21:23.222
Thread-25757 An exception occurred during request network execution
:401 Unauthorized 09-20 13:21:23.210:
E//RequestRunner.java:134(16646):
org.springframework.web.client.HttpClientErrorException: 401
Unauthorized 09-20 13:21:23.210: E//RequestRunner.java:134(16646):
    at
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
com.example.twitterapp.BearerTokenJsonRequest.loadDataFromNetwork(BearerTokenJsonRequest.java:83)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
com.example.twitterapp.BearerTokenJsonRequest.loadDataFromNetwork(BearerTokenJsonRequest.java:1)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
com.octo.android.robospice.request.RequestRunner$1.run(RequestRunner.java:197)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
java.util.concurrent.FutureTask.run(FutureTask.java:137) 09-20
13:21:23.210: E//RequestRunner.java:134(16646):     at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-20 13:21:23.210: E//RequestRunner.java:134(16646):   at
java.lang.Thread.run(Thread.java:856)

请帮忙,我在这个问题上卡了三天:/

这是我的 apistring 日志

09-23 16:10:46.209: D/JSON(29263): apistring: xvz1evFS4wEEPTGEFPHBog:L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg

对于 base64 编码的字符串

09-23 16:10:52.329: D/ENCODED(29263): Base64:       eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4
09-23 16:10:52.329: D/ENCODED(29263): OERSZHlPZw== //idk why it shows my log in 2 lines
4

1 回答 1

0

您能否将您的 MyClientKey 和 MyClientSecret 替换为 tweeter提供的示例,并确保您通过邮件发送的基本身份验证是正确的?

请替换它们并显示日志,以明确您获得了 Tweeter 所需的确切表格。

请注意:之后,您应该隔离代码以生成您发送的令牌,并使用高音扬声器的示例在单元测试中测试此代码。

于 2013-09-23T12:09:35.223 回答