final String userFacebookId = ...
new AsyncTask<Void, Void, Bitmap>()
{
@Override
protected Bitmap doInBackground(Void... params)
{
// safety check
if (userFacebookId == null)
return null;
String url = String.format(
"https://graph.facebook.com/%s/picture",
userFacebookId);
// you'll need to wrap the two method calls
// which follow in try-catch-finally blocks
// and remember to close your input stream
InputStream inputStream = new URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
// safety check
if (bitmap != null
&& !isChangingConfigurations()
&& !isFinishing())
// do what you need to do with the bitmap :)
}
}.execute();
Showing posts with label facebook. Show all posts
Showing posts with label facebook. Show all posts
Tuesday, 5 March 2013
Facebook Android SDK 3.0 - getting a user's profile picture
Here's another Facebook operation which you'd think should not only be easy to do but also be easy to find in the documentation. It certainly was easy to do, but once again wasn't so easy to find in the documentation. Oh and, strictly speaking, you don't need the Facebook SDK for this. You just need to make a HTTP GET call to the Facebook user's picture connection. (You'll need the Facebook SDK to get the user's Facebook id beforehand.)
Thursday, 28 February 2013
Facebook Android SDK 3.0 - how to log out and close a session
In the previous two Blog posts (here and here), I showed how to connect a user to their Facebook account (i.e. open a Facebook session) and how to make a Facebook API request (requesting additional Facebook permissions en route if necessary). In this final Blog post, I'll show to close the Facebook session, as follows:
public void disconnectFacebookAccount()
{
showProgressDialog("Disconnecting Facebook account...");
new AsyncTask<Void, Void, Boolean>()
{
@Override
protected Boolean doInBackground(Void... params)
{
if (Session.getActiveSession() != null)
Session.getActiveSession().closeAndClearTokenInformation();
clearFacebookInfoFromSharedPreferences();
// perform any other operations you need to do perform here
// such as clearing local database tables and so forth
return true;
}
@Override
protected void onPostExecute(Boolean result)
{
// safety check
if (isFinishing())
return;
if (result == null
|| result == false)
onFailedFacebookDisconnect();
else
onSucceededFacebookDisconnect();
}
}.execute();
}
That's it! That's the end of my Facebook Android 3.0 series. You should have enough know-how now for all kinds of Facebook API requests! :)
Facebook Android SDK 3.0 - requesting additional permissions and making an API request
At the end of the previous Blog post, I defined a
connectFacebookAccount(...)
method which connects to a user's Facebook account and opens a Facebook session for subsequent Facebook API requests. In this Blog post, I'll show how to use this method and the opened Facebook session to post to the user's feed. You'll notice in reading through the postFacebookMessage(...)
method below that it is sometimes necessary to request additional Facebook permissions before making a Facebook API request. (It's no longer possible to request all required Facebook permissions at the point of first connecting to Facebook.) Without further ado, here's the postFacebookMessage(...)
method:
/** Posts the provided message to Facebook,
* connecting to Facebook
* and requesting required permissions en route if necessary.*/
public void postFacebookMessage(final String message)
{
connectFacebookAccount(new FacebookConnectHandler()
{
@Override
public void onSuccess()
{
// safety check
if (isFinishing())
return;
showProgressDialog("Posting message to Facebook...");
// check for publish permissions
final List permissions_required = Arrays.asList(
new String[] { Constants.Facebook_Permission_PublishStream });
if (Session.getActiveSession().getPermissions() == null
|| !Session.getActiveSession().getPermissions().containsAll(
permissions_required))
{
// need to make a Session.openActiveSessionFromCache(...) call
// because of a bug in the Facebook sdk
// where a second call to get permissions
// won't result in a session callback when the token is updated
if (Session.openActiveSessionFromCache(BaseFacebookActivity.this) == null)
{
onFailedPostingFacebookMessage();
return;
}
Session.getActiveSession().addCallback(new Session.StatusCallback()
{
@Override
public void call(
Session session,
SessionState state,
Exception exception)
{
if (exception != null
|| state.equals(SessionState.CLOSED)
|| state.equals(SessionState.CLOSED_LOGIN_FAILED))
{
// didn't get required permissions
session.removeCallback(this);
// safety check
if (!isFinishing())
onFailedPostingFacebookMessage();
}
else if (state.equals(SessionState.OPENED_TOKEN_UPDATED)
&& session.getPermissions().containsAll(permissions_required))
{
// got required permissions
session.removeCallback(this);
// safety check
if (!isFinishing())
postFacebookMessage(message);
}
}
});
Session.getActiveSession().requestNewPublishPermissions(
new Session.NewPermissionsRequest(
BaseFacebookActivity.this,
permissions_required));
return;
}
// got sufficient permissions, so publish message
Bundle bundle_params = new Bundle();
bundle_params.putString("caption", "Enter your caption here"));
bundle_params.putString("description", "Enter your description here");
bundle_params.putString("link", "http://www.your-app-url.com");
bundle_params.putString("message", message);
bundle_params.putString("name", "My App for Android");
bundle_params.putString("picture", "http://www.your-app-icon-url.com");
new Request(
Session.getActiveSession(),
"me/feed",
bundle_params,
HttpMethod.POST,
new Request.Callback()
{
@Override
public void onCompleted(Response response)
{
// safety check
if (isFinishing())
return;
if (response.getError() != null
|| response.getGraphObject() == null)
{
onFailedPostingFacebookMessage();
return;
}
Object id = response.getGraphObject().getProperty("id");
if (id == null
|| !(id instanceof String)
|| TextUtils.isEmpty((String)id))
onFailedPostingFacebookMessage();
else
onSucceedPostingFacebookMessage((String)id);
}
}).executeAsync();
}
@Override
public void onFailure()
{
cancelProgressDialog();
showToast("Failed connecting to Facebook.");
}
});
}
You'll notice two methods above onSucceedPostingFacebookMessage(...)
and onFailedPostingFacebookMessage()
which you'll need to define to cancel the shown progress dialog, show a toast alerting the user to success or failure, and to perform any additional operations you require. That's it! Just one last Blog post to follow...
Facebook Android SDK 3.0 - opening a session
I've been working with the new Facebook Android SDK (version 3.0) the past two weeks and every time I thought "yes, now I've got it!", I've found something wrong with the integration of it in my apps! But this time I think I've really got it (!!) and I'm gonna do a sequence of three Blog posts to explain (1) how to connect to a Facebook user account and open a Facebook session, (2) how to obtain additional, required Facebook permissions and then perform a Facebook API request like posting to the user's feed, and (3) how to log out and close the Facebook session. I wish the official documentation better explained these core concepts but it doesn't! And this is evident in browsing developer forums like Stack Overflow where almost everybody has a different take on how to perform the above operations!! So here's my two cents...
Firstly, I like to a have single
Firstly, I like to a have single
Activity
(let's call it BaseFacebookActivity
) that has all my Facebook methods in it and which Activities that do Facebook operations are to extend. Don't forget the onActivityResult(...)
method!, as follows:
public abstract class BaseFacebookActivity
extends Activity
{
@Override
protected void onActivityResult(
int requestCode,
int resultCode,
Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(
this,
requestCode,
resultCode,
data);
}
// other methods to follow
}
Secondly, let's define an interface which will help us report back to the user when an attempt to open a Facebook session has been successful or not, as follows:
public interface FacebookConnectHandler
{
/** Method to call when the user's Facebook account
* was connected to
* and a Facebook session was opened successfully.*/
public void onSuccess();
/** Method to call when the user's Facebook account
* was not connected to
* or a Facebook session was not opened successfully.*/
public void onFailure();
}
Thirdly and finally, a method to put in your BaseFacebookActivity
which will connect to the user's Facebook account and open a Facebook session, as follows:
private void connectFacebookAccount(
final FacebookConnectHandler handler)
{
// safety check
if (!isActiveNetworkConnected())
{
handler.onFailure();
return;
}
// check whether the user already has an active session
// and try opening it if we do
// (note: making a Session.openActiveSessionFromCache(...) call
// instead of simply checking whether the active session is opened
// because of a bug in the Facebook sdk
// where successive calls to update a token
// (requesting additional permissions etc)
// don't result in a session callback)
if (Session.getActiveSession() != null
&& Session.openActiveSessionFromCache(this) != null)
{
handler.onSuccess();
return;
}
// initialise the session status callback
Session.StatusCallback callback = new Session.StatusCallback()
{
@Override
public void call(
Session session,
SessionState state,
Exception exception)
{
// safety check
if (isFinishing())
return;
// check session state
if (state.equals(SessionState.CLOSED)
|| state.equals(SessionState.CLOSED_LOGIN_FAILED))
{
clearFacebookInfoFromSharedPreferences();
// specific action for when the session is closed
// because an open-session request failed
if (state.equals(SessionState.CLOSED_LOGIN_FAILED))
{
cancelProgressDialog();
handler.onFailure();
}
}
else if (state.equals(SessionState.OPENED))
{
cancelProgressDialog();
saveFacebookInfoInSharedPreferences(
session.getAccessToken(),
session.getExpirationDate());
showToast("Succeeded connecting to Facebook");
handler.onSuccess();
}
}
};
// make the call to open the session
showProgressDialog("Connecting to Facebook...");
if (Session.getActiveSession() == null
&& getSharedPreferences().contains("facebookAccessToken")
&& getSharedPreferences().contains("facebookAccessTokenExpires"))
{
// open a session from the access token info
// saved in the app's shared preferences
String accessTokenString = getSharedPreferences().getString(
"facebookAccessToken",
"");
Date accessTokenExpires = new Date(getSharedPreferences().getLong(
"facebookAccessTokenExpires",
0));
AccessToken accessToken = AccessToken.createFromExistingAccessToken(
accessTokenString,
accessTokenExpires,
null,
null,
null);
Session.openActiveSessionWithAccessToken(this, accessToken, callback);
}
else
// open a new session, logging in if necessary
Session.openActiveSession(this, true, callback);
}
Done! In the next Blog post I'll show how to use this connectFacebookAccount(...)
method prior to making a Facebook API request like posting to the user's feed.
Wednesday, 30 January 2013
Parse Android SDK, Facebook and ProGuard
If you're logging into Facebook via the Parse Android SDK and are building your application package (apk) with ProGuard enabled, here's what you'll need to add to your
proguard-project.txt file
:
# need this for Facebook SDK
-keepattributes Signature
-dontwarn com.facebook.**
-dontwarn com.parse.**
-keep class com.facebook.** { *; }
-keep class com.parse.** { *; }
Tuesday, 24 July 2012
Android: Posting a Facebook question with options
Posting a question which has no associated options is easy to figure out from the Facebook documentation. Posting a question which does have associated options I struggled to find a single example for and figured out by pure luck, a shot in the dark! Solution below:
Bundle params = new Bundle();
params.putString("questions", "This is a question.");
params.putString("options", "[\"Option 1\", \"Option 2\"]");
new AsyncFacebookRunner(myFacebookObject)
.request("me/questions", params, "POST" myRequestListener);
Wednesday, 17 August 2011
Getting started with the Android Facebook SDK
First thing to do is to link the Android Facebook SDK to your Android project.
Next, reference a Facebook object somewhere in your Android project code and add a method to initialise it with your Facebok app id. For example:
private static Facebook facebook;
public static void initialise(String facebookAppId) { facebook = new Facebook(facebookAppId); }
Before you can get or post any data from Facebook, you will need to initialise your Facebook object instance (using the method above for example) and then call one of the Facebook class authorize(...) methods for your app to be granted permissions to make Facebook api calls. For example:
facebook.authorize(MyActivity.this, new String[] { "publish_stream" }, new Facebook.DialogListener() { /* add implementation of DialogListener interface methods */ });
Once permissions are granted (i.e. authorisation is successful), you can start making requests to the Facebook api using the AsyncFacebookRunner class. For example, to get details of the current user, do the following:
new AsyncFacebookRunner(facebook).request("me", new AsyncFacebookRunner.RequestListener() { /* add implementation of RequestListener interface methods */ }
Or, similarly, to post a message to the current user's wall, do the following, where you can find an example of how to set up bundleParams in the comments below:
new AsyncFacebookRunner(facebook).request("me/feed", bundleParams, "POST", new AsyncFacebookRunner.RequestListener() { /* add implementation of RequestListener interface methods */ }, new Object());
Lastly, to log out the currently signed in user from Facebook, call the Facebook class logout(Context) method, passing the Context that was used to do the login (authorisation). For example:
facebook.logout(MyActivity.this);
That's it! That's the basics of using the Android Facebook SDK.
Next, reference a Facebook object somewhere in your Android project code and add a method to initialise it with your Facebok app id. For example:
private static Facebook facebook;
public static void initialise(String facebookAppId) { facebook = new Facebook(facebookAppId); }
Before you can get or post any data from Facebook, you will need to initialise your Facebook object instance (using the method above for example) and then call one of the Facebook class authorize(...) methods for your app to be granted permissions to make Facebook api calls. For example:
facebook.authorize(MyActivity.this, new String[] { "publish_stream" }, new Facebook.DialogListener() { /* add implementation of DialogListener interface methods */ });
Once permissions are granted (i.e. authorisation is successful), you can start making requests to the Facebook api using the AsyncFacebookRunner class. For example, to get details of the current user, do the following:
new AsyncFacebookRunner(facebook).request("me", new AsyncFacebookRunner.RequestListener() { /* add implementation of RequestListener interface methods */ }
Or, similarly, to post a message to the current user's wall, do the following, where you can find an example of how to set up bundleParams in the comments below:
new AsyncFacebookRunner(facebook).request("me/feed", bundleParams, "POST", new AsyncFacebookRunner.RequestListener() { /* add implementation of RequestListener interface methods */ }, new Object());
Lastly, to log out the currently signed in user from Facebook, call the Facebook class logout(Context) method, passing the Context that was used to do the login (authorisation). For example:
facebook.logout(MyActivity.this);
That's it! That's the basics of using the Android Facebook SDK.
Thursday, 24 February 2011
How to debug a facebook app using eclipse (gwt)
Steps to get a facebook app (written using google web toolkit) to run on the Visual Studio server and be picked up by the Eclipse debugger:
(1) Go to app developer page (e.g. http://www.facebook.com/developers/apps.php?app_id=168754973148732).
(2) Check and edit app settings as necessary. Look out for Facebook Integration settings (and Canvas URL setting) in particular. (Example Canvas URL value: http://127.0.0.1:21024/fb/)
(3) Navigate to app (Canvas Page) and append "index.html" or similar page to url. (Example Canvas Page value: http://apps.facebook.com/gskallitest/) Reload page.
(4) Use Google Chrome or Mozilla Firefox 'Inspect Element' tool to select the app in the page (it might not be visible but will be present in the html markup).
(5) Find the app iframe in the page html markup and pull out the src url. Bung in between "...index.html?" and "signed_request=..." the text "gwt.codesvr=127.0.0.1:9997&".
(6) Stick this modified url into your browser and assuming you have your gwt project running/debugging on Eclipse you're good to debug :)>
Note: When running the facebook project on eclipse (as a web application), uncheck the 'run built-in server' option from the Debug Configurations dialog ('Server' tab) and specify instead the server url to run from ('GWT' tab), i.e. the Canvas URL as specified in step 2.
ps. Remember to change the facebook app settings (step 2) back after testing/debugging. In particular: the Canvas URL setting which should be http://alli2.gsk.kickstartdigital.co.uk/fb/ or similar.
(1) Go to app developer page (e.g. http://www.facebook.com/developers/apps.php?app_id=168754973148732).
(2) Check and edit app settings as necessary. Look out for Facebook Integration settings (and Canvas URL setting) in particular. (Example Canvas URL value: http://127.0.0.1:21024/fb/)
(3) Navigate to app (Canvas Page) and append "index.html" or similar page to url. (Example Canvas Page value: http://apps.facebook.com/gskallitest/) Reload page.
(4) Use Google Chrome or Mozilla Firefox 'Inspect Element' tool to select the app in the page (it might not be visible but will be present in the html markup).
(5) Find the app iframe in the page html markup and pull out the src url. Bung in between "...index.html?" and "signed_request=..." the text "gwt.codesvr=127.0.0.1:9997&".
(6) Stick this modified url into your browser and assuming you have your gwt project running/debugging on Eclipse you're good to debug :)>
Note: When running the facebook project on eclipse (as a web application), uncheck the 'run built-in server' option from the Debug Configurations dialog ('Server' tab) and specify instead the server url to run from ('GWT' tab), i.e. the Canvas URL as specified in step 2.
ps. Remember to change the facebook app settings (step 2) back after testing/debugging. In particular: the Canvas URL setting which should be http://alli2.gsk.kickstartdigital.co.uk/fb/ or similar.
Subscribe to:
Posts (Atom)