开发者

Android Jtwitter, authentication issues

开发者 https://www.devze.com 2023-03-22 01:50 出处:网络
Hello I am having difficulty using the JTwitter functions to authenticate with my twitter application. I always get a \"TwitterException\"

Hello I am having difficulty using the JTwitter functions to authenticate with my twitter application. I always get a "TwitterException"

Here is my method

OAuthSignpostClient oauthClient = new OAuthSignpostClient(consumerKey, 
            privateKey, "oob");

a) I don't know what the "oob" value SHOULD be, it is the "callbackURL" and in my application on twitter it says "callBack URL: none" so I have tried putting "none", "None", and null where "oob" with no differing results.

then the rest is boilerplate

 Intent i = new Intent(Intent.ACTION_VIEW, 
    Uri.parse(oauthClient.authorizeUrl().toString()));
    startActivity(i);
    // get the pin
    String v = oauthClient.askUser("Please enter the verification PIN from Twitter");
    oauthClient.setAuthorizationCode(v);
    // Store the authorisation token details for future use
    String[] accessToken = oauthClient.getAccessToken();
    // Next time we can use new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET, 
    //        accessToken[0], accessToken[1]) to avoid authenticating again.

    EditText twitterText = (EditText)findViewById(R.id.twitterText);
    twitterText.getText();

    // Make a Twitter object
开发者_运维知识库    Twitter twitter = new Twitter(null, oauthClient);
    // Print Daniel Winterstein's status
    //System.out.println(twitter.getStatus("winterstein"));
    // Set my status
    twitter.setStatus(twitterText.getText());

at this point, I'm simply not sure on how to make this work. Wish I could be more verbose about it, but it has something to do with the authentication. Online things I've seen haven't been helpful


Try this as your callback url:

OAuthSignpostClient oauthClient = 
    new OAuthSignpostClient(consumerKey, privateKey, "callback://twitter");  

Remember! After:

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));

Do the following

override onResume() to get the verification code

protected void onResume() {
    super.onResume();
    if (this.getIntent()!=null && this.getIntent().getData()!=null){
        Uri uri = this.getIntent().getData();
        if (uri != null && uri.toString().startsWith("callback://twitter")) {
            //Do whatever you want
            //usually use uri.getQueryParameter(someString);
            //test what could be someString using Log.v("",uri.toString());
            //you want the Authorization code which is a couple of numbers
            //so you could use oauthClient.setAuthorizationCode(v); 
            //and finally initialise Twitter
        }
    }
}

You can use uri.getQueryParameterNames() to get the parameter String names.


@BobVork

I'd just like to add that you'll need to have a callback URL set in the Settings tab for your Twitter app (on twitter.com).

It doesn't even matter what you put in the field, but if it's empty, callback urls will not work.


I'm not sure that's the problem, but it seems like you never set the user name for the twitter object.


Well, I don't know how it is with the Jtwitter API and I don't know enough Java at this point to really understand the inner workings of this code, but have you tried, instead of NULL or None, to try ? instead. That is the callback used with the restful API when making calls from Javascript. Try it out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号