在Android的OAuth的实例状态 [英] OAuth instance state in Android

查看:114
本文介绍了在Android的OAuth的实例状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用OAuth在一个Android应用程序。我有它工作正常,但在验证阶段有时会碰到一个问题。在Android中,我启动浏览器,登录并验证用户。然后回调URL会重定向到我的申请。

I'm trying to use OAuth in an Android app. I have it working correctly but have sometimes run into a problem during the authentication phase. In Android, I launch the browser for the user to login and authenticate. Then the callback url will redirect back to my application.

这是问题所在。我的应用程序有一个OAuth的消费者和提供者作为我的主类的成员。当启动浏览器进行身份验证,有时我的主要活动是丢弃,以节省内存。当回调URL重新推出我的主要活动中,提供者和使用者都是新情况,当我尝试做一个请求到API为此不起作用。如果主Activiy在验证阶段没有被释放,那么一切工作正常,因为我还在与原来的消费者和提供者。

Here is the problem. My application has a OAuth consumer and provider as members of my main class. When the browser is launched for authentication, sometimes my main Activity is discarded to save memory. When the callback url relaunches my main Activity, the provider and consumer are new instances and therefor don't work when I try to make a request to the api. If the main Activiy was not freed during the authentication phase, then everything works correctly because I'm still working with the original consumer and provider.

我试着使用的onSaveInstanceState()和onRestoreInstanceState(),但都没有成功。看来,当我的回调URL处理的onRestoreInstanceState()将不会调用。似乎直奔onResume()。

I tried using onSaveInstanceState() and onRestoreInstanceState(), but haven't been successful. It seems the onRestoreInstanceState() is not called when my callback url is handled. Seems to go straight to the onResume().

什么是正确的方法坚持消费者和提供者在这种情况下?

What is the correct method for persisting the consumer and provider in this case?

推荐答案

完整保存/恢复解决方案

除了 request_token token_secret isOauth10a()状态是很重要的,在提供者得到恢复。有可能是在未来更多的状态信息。因此,我喜欢坚持和负载的解决方案是最好的。

Apart from the request_token and token_secret, the isOauth10a() state is important to be restored in the provider. There could be more state information in the future. Hence, I like the persist and load solution the best.

我延长 GrkEngineer 的解决方案更加完善。它保存/恢复提供者和消费者,处理所有的异常,并设置HttpClient的同时恢复。

I extended GrkEngineer's solution to be more complete. It saves / restores both the provider and consumer, handles all exceptions, and sets the httpClient while restoring.

protected void loadProviderConsumer()
{
  try {
    FileInputStream fin = this.openFileInput("tmp_provider.dat");
    ObjectInputStream ois = new ObjectInputStream(fin);
    provider = (CommonsHttpOAuthProvider) ois.readObject();
    provider.setHttpClient(httpClient);
    ois.close();
    fin.close();

    fin = this.openFileInput("tmp_consumer.dat");
    ois = new ObjectInputStream(fin);
    consumer = (CommonsHttpOAuthConsumer) ois.readObject();
    ois.close();
    fin.close();

    Log.d("OAuthTwitter", "Loaded state");
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (StreamCorruptedException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } catch (ClassNotFoundException e) {
    e.printStackTrace();
  }
}

protected void persistProviderConsumer()
{

  try {
    FileOutputStream fout = this.openFileOutput("tmp_provider.dat", MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(provider);
    oos.close();
    fout.close();

    fout = this.openFileOutput("tmp_consumer.dat", MODE_PRIVATE);
    oos = new ObjectOutputStream(fout);
    oos.writeObject(consumer);
    oos.close();
    fout.close();

    Log.d("OAuthTwitter", "Saved state");
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

我已经测试了这个code和它的作品。

I have tested this code and it works.

这篇关于在Android的OAuth的实例状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆