这是我第一次发布到stackOverflow,我想通过oauth2(仅应用程序身份验证)从twitter获取持有者令牌并遵循here的指示,但我的代码返回错误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未经授权。我确保消费者密钥和密钥是正确的,尝试url编码Base64编码的字符串
这是我的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
答案 0 :(得分:0)
您可以使用examples provided by tweeter替换MyClientKey和MyClientSecret,并确保您通过邮寄发送的基本身份验证是正确的吗?
请更换它们并显示日志,以明确表示您已获得Tweeter所需的确切表格。
请注意:之后,您应该隔离代码以生成您发送的令牌,并使用tweeter的示例在单元测试中测试此代码。