Skip to content

Commit

Permalink
Added the refresh token method.
Browse files Browse the repository at this point in the history
Summary: This allows developers to silently refresh their access token by
calling Facebook.refreshToken method. This SDK will try to call our Facebook
Android App which will handle the API call.

Test Plan:
This requires adding a refresh token service to our sdk. See D364973.
After that try using the new Hackbook example.

Reviewers: jimbru, raghuc1, brent, dalves, ttung, yariv

Reviewed By: jimbru

CC: dalves, jimbru

Differential Revision: https://phabricator.fb.com/D366540

Task ID: 799996
  • Loading branch information
Kamil Kraszewski authored and Kamil Kraszewski committed Dec 30, 2011
1 parent 50e4fd5 commit a1ebdc9
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 9 deletions.
67 changes: 67 additions & 0 deletions examples/Hackbook/res/layout/token_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black">
<TextView
android:id="@+id/tokenLabel"
android:text="@string/access_token_label"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingLeft="3dp" />
<EditText
android:id="@+id/tokenEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false" />
<TextView
android:id="@+id/tokenExpiresLabel"
android:text="@string/access_token_expires_label"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingLeft="3dp" />
<EditText
android:id="@+id/tokenExpiresEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/grey" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/refresh_button"
android:text="@string/refresh_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/grey" />
<TextView
android:id="@+id/tip_label"
android:text="@string/tip_label"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingLeft="3dp" />
<TextView
android:id="@+id/usefulTip"
android:text="@string/refresh_token_tip"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingLeft="3dp" />
</LinearLayout>
8 changes: 8 additions & 0 deletions examples/Hackbook/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="target_url">Target URL:</string>
<string name="exception">Exception: </string>
<string name="tip_label">Tip:</string>
<string name="error">Error</string>
<string name="facebook_error">Facebook Error: </string>
<string name="api_response">Api Response:</string>
<string name="view_source">View Source</string>
Expand Down Expand Up @@ -77,4 +78,11 @@
<string name="enable_gps_title">Enable GPS</string>
<string name="enable_gps">Please enable GPS to get nearby places and check-in</string>
<string name="gps_settings">GPS Settings</string>
<string name="refresh_token_title">Refresh Token</string>
<string name="refresh_button">Refresh</string>
<string name="refresh_button_pending">Refreshing…</string>
<string name="refresh_token_tip">In most cases the access token should be refreshed silently when the application is running (for example see Hackbook onResume method).</string>
<string name="refresh_token_binding_error">Binding to the Facebook Android Application failed (is it installed?).</string>
<string name="access_token_label">Current access token:</string>
<string name="access_token_expires_label">Token expires at:</string>
</resources>
22 changes: 17 additions & 5 deletions examples/Hackbook/src/com/facebook/android/Hackbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Hackbook extends Activity implements OnItemClickListener {

private ListView list;
String[] main_items = { "Update Status", "App Requests", "Get Friends", "Upload Photo",
"Place Check-in", "Run FQL Query", "Graph API Explorer" };
"Place Check-in", "Run FQL Query", "Graph API Explorer", "Token Refresh" };
String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins",
"photo_upload" };

Expand Down Expand Up @@ -117,9 +117,13 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onResume() {
super.onResume();
if (Utility.mFacebook != null && !Utility.mFacebook.isSessionValid()) {
mText.setText("You are logged out! ");
mUserPic.setImageBitmap(null);
if(Utility.mFacebook != null) {
if (!Utility.mFacebook.isSessionValid()) {
mText.setText("You are logged out! ");
mUserPic.setImageBitmap(null);
} else {
Utility.mFacebook.extendAccessTokenIfNeeded(this, null);
}
}
}

Expand Down Expand Up @@ -210,7 +214,7 @@ public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
* Source Tag: friends_tag You can get friends using
* graph.facebook.com/me/friends, this returns the list sorted by
* UID OR using the friend table. With this you can sort the way you
* want it.
* want it.
* Friend table - https://developers.facebook.com/docs/reference/fql/friend/
* User table - https://developers.facebook.com/docs/reference/fql/user/
*/
Expand Down Expand Up @@ -363,6 +367,14 @@ public void onClick(DialogInterface dialog, int which) {
startActivity(myIntent);
break;
}

case 7: {
if(!Utility.mFacebook.isSessionValid()) {
Util.showAlert(this, "Warning", "You must first log in.");
} else {
new TokenRefreshDialog(Hackbook.this).show();
}
}
}
}

Expand Down
96 changes: 96 additions & 0 deletions examples/Hackbook/src/com/facebook/android/TokenRefreshDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.facebook.android;

import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TokenRefreshDialog extends Dialog {

private EditText tokenEdit, tokenExpiresEdit;
private TextView mUsefulTip;
private Button mRefreshButton;
private Activity activity;

public TokenRefreshDialog(Activity activity) {
super(activity);
this.activity = activity;
setTitle(R.string.refresh_token_title);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.token_refresh);

tokenEdit = (EditText) findViewById(R.id.tokenEdit);
tokenEdit.setText(Utility.mFacebook.getAccessToken());

tokenExpiresEdit = (EditText) findViewById(R.id.tokenExpiresEdit);
setExpiresAt(Utility.mFacebook.getAccessExpires());

mUsefulTip = (TextView) findViewById(R.id.usefulTip);
mUsefulTip.setMovementMethod(LinkMovementMethod.getInstance());
mRefreshButton = (Button) findViewById(R.id.refresh_button);

mRefreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonState(false);
RefreshTokenListener listener = new RefreshTokenListener();
if (!Utility.mFacebook.extendAccessToken(activity, listener)) {
listener.onError(new Error(
activity.getString(R.string.refresh_token_binding_error)));
}
}
});
}

private class RefreshTokenListener implements Facebook.ServiceListener {

@Override
public void onFacebookError(FacebookError e) {
changeButtonState(true);
String title = String.format(activity.getString(R.string.facebook_error) + "%d",
e.getErrorCode());
Util.showAlert(activity, title, e.getMessage());
}

@Override
public void onError(Error e) {
changeButtonState(true);
Util.showAlert(activity, activity.getString(R.string.error), e.getMessage());
}

@Override
public void onComplete(Bundle values) {
changeButtonState(true);

// The access_token and expires_at values are automatically updated,
// so they can be obtained by using:
// - Facebook.getAccessToken()
// - Facebook.getAccessExpires()
// methods, but we can also get them from the 'values' bundle.
tokenEdit.setText(values.getString(Facebook.TOKEN));
setExpiresAt(values.getLong(Facebook.EXPIRES));
}
}

private void changeButtonState(boolean enabled) {
mRefreshButton.setEnabled(enabled);
mRefreshButton.setText(enabled ? R.string.refresh_button : R.string.refresh_button_pending);
}

private void setExpiresAt(long time) {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
tokenExpiresEdit.setText(dateFormat.format(new Date(time)));
}
}
Loading

0 comments on commit a1ebdc9

Please sign in to comment.