I am new to development facebook. I want loginto facebookand allow the user to allow my application to publish photos and more. but when the user is loginsuccessful, he twice displays the authorization dialog. Here is my code
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Log.d("", "usman: session is opened");
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
Log.d("", "onCompleted called");
if (user != null) {
Log.d(MyConstants.TAG,
"facebook Hello : "
+ user.getName() + "!");
publishStory();
} else {
Toast.makeText(
getApplicationContext(),
"Unable to Post,Try Again later",
Toast.LENGTH_LONG).show();
}
}
});
} else {
Log.d("", "usman: Session is not open");
}
}
});
private void publishStory() {
Log.d("", "usman: in publishStory");
Session session = Session.getActiveSession();
if (session != null) {
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(fileImage);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
Bundle postParams = new Bundle();
postParams.putString("name", "London For Less Postcard");
postParams.putString("caption",
"Build great social apps and get more installs.");
postParams
.putString(
"description",
"The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putByteArray("picture", data);
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i("FB", "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(
SendPostCardActivity.this
.getApplicationContext(),
error.getErrorMessage(), Toast.LENGTH_SHORT)
.show();
Log.d("", "usman: Error");
} else {
Toast.makeText(
SendPostCardActivity.this
.getApplicationContext(),
"Posted on facebook wall successfully",
Toast.LENGTH_LONG).show();
Log.d("", "usman: Finish");
finish();
}
}
};
Request request = new Request(session, "me/photos", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
source
share