2

Ok so I'm new to OAuth and I'm creating an app that integrates with Twitter.

I'm using Twitter4j and following their instructions. No problems there.

  • I can ask the user to authorize my app
  • I exchange a Request Token for an Access Token
  • Once the user athorizes the app I serialize the object
  • Here's how the serialization occurs

    FileOutputStream fos = getContext().openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(accessToken);
    oos.close();
    

So when it's time to use the Access Token again, I simply desiralize the object and then assign to a new Twitter object and invoke a status update, as below:

AccessToken twitterToken = objectDeserialization();
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(twitterApiKey, twitterApiSecret);
twitter.setOAuthAccessToken(twitterToken);
twitter4j.Status status = twitter.updateStatus("This is sparta! :)");

The problem is that I'm getting a 401.

Everywhere I read I'm convinced that I'm not supposed to go through the whole token exchange thing again. Am I wrong?

Maybe I should simply store the Access Token and the Access Token Secret and create a new object from scratch instead of deserializing an old one?

Appreciate your help :-)

4

1 回答 1

2

好的,我设法解决了我的问题,解决方案是:只保存令牌和令牌秘密,然后创建一个新对象。

AccessToken twitterToken = objectDeserialization();
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(twitterApiKey, twitterApiSecret);
twitter.setOAuthAccessToken(new AccessToken(twitterToken.getToken(), twitterToken.getTokenSecret()));
twitter4j.Status status = twitter.updateStatus("This is sparta! :)");
于 2012-11-06T21:50:00.020 回答