Permalink
Browse files

Get the User's Photos

  • Loading branch information...
dragosholban committed Mar 14, 2018
1 parent 0357dd7 commit 0e795231925e6c974fb0dee295a49942d1f125b9
Showing with 31 additions and 1 deletion.
  1. +31 −1 app/src/main/java/dragosholban/com/bestphotos/MainActivity.java
@@ -5,13 +5,20 @@
import android.os.Bundle;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
@@ -23,14 +30,37 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
LoginButton loginButton = findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("email"));
loginButton.setReadPermissions(Arrays.asList("user_photos"));
// Callback registration
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "Facebook login token: " + loginResult.getAccessToken().getToken());
// Check permissions
Boolean granted = false;
Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
for (String permission : permissions) {
if (permission.equals("user_photos")) {
granted = true;
}
}
if (granted) {
GraphRequest.Callback callback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject json = response.getJSONObject();
Log.d(TAG, "Photos: " + json.toString());
}
};
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500", null, HttpMethod.GET, callback);
request.executeAsync();
}
}
@Override

0 comments on commit 0e79523

Please sign in to comment.