好的,所以,我正在为Android制作Tumblr客户端,我一直在尝试并且未能让OAuth工作一周左右。以下是它的发展方式:
用户启动应用。主要活动onCreate这样做:
settings = getSharedPreferences(PREFS_NAME, 0);
authToken=settings.getString("OauthToken", "none");
authTokenSecret=settings.getString("OauthSecret", "none");
if(authToken=="none" || authTokenSecret=="none"){
Intent i = new Intent(getApplicationContext(),Authentication.class);
startActivity(i);
}
这将启动包含WebView的身份验证活动。该活动成功获取请求令牌,并将WebView发送到Tumblr登录屏幕。要求用户允许应用访问他们的数据,他们按下允许,然后我的WebViewClient捕获回调URL,并使用它执行此操作:
String[] token = helper.getVerifier(url);
if (token != null) {
try {
String accessToken[] = helper.getAccessToken(token[1]);
editor.putString("OauthToken", accessToken[0]);
editor.putString("OauthSecret", accessToken[1]);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
finish();
帮助程序类的getAccessToken和getVerifier看起来像这样:
public String[] getVerifier(String myUrl) {
// extract the token if it exists
Uri uri = Uri.parse(myUrl);
if (uri == null) {
return null;
}
String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");
return new String[] { token, verifier };
}
public String[] getAccessToken(final String verifier)
throws OAuthMessageSignerException, OAuthNotAuthorizedException,
OAuthExpectationFailedException, OAuthCommunicationException {
new Thread(new Runnable() {
public void run() {
try {
mProvider.retrieveAccessToken(mConsumer, verifier);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
return new String[] {
mConsumer.getToken(), mConsumer.getTokenSecret()
};
}
然后我终于回到主应用程序屏幕并尝试进行我的第一次API调用,以获取用户仪表板上最近的十个帖子:
OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(MainView.authToken, MainView.authTokenSecret);
HttpGet request = new HttpGet("http://api.tumblr.com/v2/user/dashboard?limit=10");
myConsumer.sign(request);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
然而,我没有像我应该那样得到一个不错的JSON响应,而是得到了这个:
10-20 16:36:18.110: D/Result(22817): {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
那我哪里出错了?感谢
答案 0 :(得分:2)
我已成功使用带有Appache HttpCommons GET / POST / PUT的路标库。
首先,我使用以下代码(ActivityLogin.java
)启动了WebView以进行登录:
private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);
private static OAuthProvider provider = new DefaultOAuthProvider (AppConfig.requestURL, AppConfig.accessURL, AppConfig.authURL);
private void logMeIn() throws ...{
String authUrl = provider.retrieveRequestToken(consumer,AppConfig.CALLBACK_URL);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}
然后,我使用了onNewIntent(Intent)
来接收先前发布的Activity
的OAuth回调:
AppConfig.java
代码snipet:
/** OAuth Authorization URL */
public static final String authURL = mainOAuthUrl+"/authorize/?theme=android";
/** OAuth Request URL */
public static final String requestURL = mainOAuthUrl+"/request/token/";
/** OAuth Access URL */
public static final String accessURL = mainOAuthUrl+"/access/token/";
/** OAuth CALLback URL*/
public static final String CALLBACK_URL = "yourapp://twitt";
ActivityLogin.java
代码snipet:
protected void onNewIntent(Intent intent) {
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(AppConfig.CALLBACK_URL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
provider.retrieveAccessToken(consumer, verifier);
Data.OAuthAccessKey = consumer.getToken();
Data.OAuthAccessSecret = consumer.getTokenSecret();
}
}
然后,我的连接代码如下:
public HttpResponse sampleOauthConnect(String url) throws ...{
/** setup some connection params */
HttpContext context = new BasicHttpContext();
HttpRequestBase request = new HttpGet(url);
if (Data.OAuthConsumer == null)
Data.OAuthConsumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);
if (Data.OAuthAccessKey == null || Data.OAuthAccessSecret == null)
throw new LoginErrorException(LoginErrorException.NOT_LOGGED_IN);
Data.OAuthConsumer.setTokenWithSecret(Data.OAuthAccessKey, Data.OAuthAccessSecret);
try {
Data.OAuthConsumer.sign(request);
} catch (OAuthMessageSignerException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
} catch (OAuthExpectationFailedException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
} catch (OAuthCommunicationException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
}
HttpClient client = new DefaultHttpClient();
/** finally execute this request */
return client.execute(request, context);
}
我可能在复制粘贴期间错过了一些内容,只需告诉我此解决方案是否适合您。我正在使用singpost 1.2.1.1
答案 1 :(得分:1)
目前,Tumblr不支持MultipartEntity
,因此您必须使用NameValuePairs
添加参数。
如果您想发布照片,您必须将该照片转换为ARRAY并对其进行网址编码!