Android Tumblr Oauth-signpost 401

我怕爱的太早我们不能终老 提交于 2019-12-03 15:48:34

I've successfully used signpost library with Appache HttpCommons GET/POST/PUTs.

Firstly, I've launched the WebView for login purpose, using the following code (ActivityLogin.java):

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)));
}

Then, I've used onNewIntent(Intent) for receiving OAuth callbacks from the previously launched Activity:

AppConfig.java code 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 code 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();
    }

}

And then, my connection code looks like that:

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);
}

I may have missed something during copy-paste, just tell me if this solution will work for you. I'm using singpost 1.2.1.1

Thanh_ktm

Currently, Tumblr doesn't support MultipartEntity, so you have to use NameValuePairs to add params.

If you want to post a photo, you MUST convert that photo to an ARRAY and URL encode it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!