Permalink
Browse files

come back when user authorise and use sharedpreference to help rememb…

…er key and secret
  • Loading branch information...
warmwind committed May 27, 2012
1 parent abff1b8 commit 4f7705c7d2f68d5fe8810fe0e12a64de25955685
View
@@ -33,6 +33,12 @@
<activity android:name=".SigninScreen"
android:theme="@android:style/Theme.Light.NoTitleBar">
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW" />
+ <category android:name="android.intent.category.DEFAULT" />
+ <category android:name="android.intent.category.BROWSABLE" />
+ <data android:scheme="vtbapp-doudou"/>
+ </intent-filter>
</activity>
View
@@ -35,16 +35,17 @@
public static final int book_rate=0x7f080004;
public static final int book_title=0x7f080002;
public static final int email=0x7f08000a;
- public static final int menu_more=0x7f080010;
- public static final int menu_signin=0x7f080011;
+ public static final int menu_more=0x7f080011;
+ public static final int menu_signin=0x7f080012;
+ public static final int myinfo_bt=0x7f08000d;
public static final int password=0x7f08000b;
public static final int search=0x7f080009;
public static final int search_bar=0x7f080007;
public static final int search_progress_bar=0x7f080008;
public static final int signin_bt=0x7f08000c;
- public static final int toast=0x7f08000d;
- public static final int toast_image=0x7f08000e;
- public static final int toast_message=0x7f08000f;
+ public static final int toast=0x7f08000e;
+ public static final int toast_image=0x7f08000f;
+ public static final int toast_message=0x7f080010;
}
public static final class layout {
public static final int book_details_web=0x7f030000;
View
@@ -37,5 +37,10 @@
android:layout_height="wrap_content"
android:text="Sign in"
/>
+ <Button android:id="@+id/myinfo_bt"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="My Info"
+ />
</LinearLayout>
@@ -2,6 +2,7 @@
import android.app.Activity;
import android.content.Intent;
+import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
@@ -12,55 +13,93 @@
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.basic.UrlStringRequestAdapter;
import oauth.signpost.http.HttpRequest;
-import oauth.signpost.signature.HmacSha1MessageSigner;
-import oauth.signpost.signature.QueryStringSigningStrategy;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class SigninScreen extends Activity {
- public static final int OAUTH_CODE = 1;
+ public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
+ public static final String ACCESS_TOKEN_SECRET = "ACCESS_TOKEN_SECRET";
+ public static final String REQUEST_TOKEN = "REQUEST_TOKEN";
+ public static final String REQUEST_TOKEN_SECRET = "REQUEST_TOKEN_SECRET";
private DefaultOAuthProvider authProvider;
private OAuthConsumer consumer;
+ private String CONSUMER_SECRET;
+ private String CONSUMER_KEY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
- EditText emailInput = (EditText) findViewById(R.id.email);
- EditText passwordInput = (EditText) findViewById(R.id.password);
Button signinBt = (Button) findViewById(R.id.signin_bt);
+ CONSUMER_KEY = "0d5f0a33b677be10281d1e9b23673a30";
+ CONSUMER_SECRET = "d66dc447cdfa7eeb";
+ consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
+ CONSUMER_SECRET);
+ authProvider = new DefaultOAuthProvider("http://www.douban.com/service/auth/request_token", "http://www.douban.com/service/auth/access_token", "http://www.douban.com/service/auth/authorize");
signinBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
- String CONSUMER_KEY = "0d5f0a33b677be10281d1e9b23673a30";
- String CONSUMER_SECRET = "d66dc447cdfa7eeb";
- consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
- CONSUMER_SECRET);
- consumer.setMessageSigner(new HmacSha1MessageSigner());
- consumer.setSigningStrategy(new QueryStringSigningStrategy());
try {
- authProvider = new DefaultOAuthProvider("http://www.douban.com/service/auth/request_token", "http://www.douban.com/service/auth/access_token", "http://www.douban.com/service/auth/authorize");
- String url1 = authProvider.retrieveRequestToken(consumer, null);
+ String url1 = authProvider.retrieveRequestToken(consumer, "vtbapp-doudou:///");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url1));
+
+ SharedPreferences preferences = getPreferences(MODE_PRIVATE);
+ SharedPreferences.Editor edit = preferences.edit();
+ edit.putString(REQUEST_TOKEN, consumer.getToken());
+ edit.putString(REQUEST_TOKEN_SECRET, consumer.getTokenSecret());
+ edit.commit();
+
startActivity(browserIntent);
- authProvider.retrieveAccessToken(consumer, null);
- String accessToken = consumer.getToken();
+
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ Button myInfoBn = (Button) findViewById(R.id.myinfo_bt);
+ myInfoBn.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ try {
+ SharedPreferences preferences = getPreferences(MODE_PRIVATE);
+ consumer.setTokenWithSecret(preferences.getString(ACCESS_TOKEN, ""), preferences.getString(ACCESS_TOKEN_SECRET, ""));
+
HttpRequest request = consumer.sign(new UrlStringRequestAdapter("http://api.douban.com/people/%40me"));
String s1 = EntityUtils.toString(new DefaultHttpClient().execute(new HttpGet(request.getRequestUrl())).getEntity());
System.out.println("------------s1 = " + s1);
-
-
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ try {
+ Uri uri = this.getIntent().getData();
+ if (uri != null) {
+ SharedPreferences preferences = getPreferences(MODE_PRIVATE);
+
+ consumer.setTokenWithSecret(preferences.getString(REQUEST_TOKEN, ""), preferences.getString(REQUEST_TOKEN_SECRET, ""));
+ authProvider.retrieveAccessToken(consumer, null);
+ SharedPreferences.Editor edit = preferences.edit();
+ edit.putString(ACCESS_TOKEN, consumer.getToken());
+ edit.putString(ACCESS_TOKEN_SECRET, consumer.getTokenSecret());
+ edit.commit();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
}
@@ -63,7 +63,7 @@ public AbstractOAuthConsumer(String consumerKey, String consumerSecret) {
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
setMessageSigner(new HmacSha1MessageSigner());
- setSigningStrategy(new AuthorizationHeaderSigningStrategy());
+ setSigningStrategy(new QueryStringSigningStrategy());
}
public void setMessageSigner(OAuthMessageSigner messageSigner) {
@@ -10,11 +10,6 @@
*/
package oauth.signpost;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.util.HashMap;
-import java.util.Map;
-
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
@@ -24,6 +19,9 @@
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* ABC for all provider implementations. If you're writing a custom provider,
* you will probably inherit from this class, since it takes a lot of work from

0 comments on commit 4f7705c

Please sign in to comment.