use of com.facebook.GraphRequest in project YourAppIdea by Michenux.
the class FacebookDelegate method requestUserData.
public void requestUserData() {
GraphRequest request = GraphRequest.newMeRequest(this.mAccessToken, (object, response) -> {
try {
if (response.getError() == null) {
User currentUser = new User();
// id from public_profile
currentUser.setUserId(object.getString("id"));
// id from public_profile
currentUser.setUserName(object.getString("id"));
// first_name from public_profile
currentUser.setFirstName(object.getString("first_name"));
// last_name from public_profile
currentUser.setLastName(object.getString("last_name"));
// name from public_profile
currentUser.setDisplayName(object.getString("name"));
// name from email
currentUser.setMail(object.getString("email"));
currentUser.setProviderDisplayName("Facebook");
currentUser.setProvider(PROVIDER_NAME);
mUserHelper.setCurrentUser(currentUser);
if (mUserSessionCallback != null) {
mUserSessionCallback.onLogin();
}
} else {
Log.e(YourApplication.LOG_TAG, "Error facebook graph request: " + response.getError().toString());
}
} catch (JSONException e) {
Log.e(YourApplication.LOG_TAG, "Error reading facebook profile", e);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,name,email");
request.setParameters(parameters);
request.executeAsync();
}
use of com.facebook.GraphRequest in project FirebaseUI-Android by firebase.
the class FacebookProvider method onSuccess.
@Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
FacebookRequestError requestError = response.getError();
if (requestError != null) {
Log.e(TAG, "Received Facebook error: " + requestError.getErrorMessage());
onFailure(new Bundle());
return;
}
if (object == null) {
Log.w(TAG, "Received null response from Facebook GraphRequest");
onFailure(new Bundle());
} else {
try {
String email = object.getString("email");
onSuccess(email, loginResult);
} catch (JSONException e) {
Log.e(TAG, "JSON Exception reading from Facebook GraphRequest", e);
onFailure(new Bundle());
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class ShareApi method shareLinkContent.
private void shareLinkContent(final ShareLinkContent linkContent, final FacebookCallback<Sharer.Result> callback) {
final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
final JSONObject data = response.getJSONObject();
final String postId = (data == null ? null : data.optString("id"));
ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
}
};
final Bundle parameters = new Bundle();
this.addCommonParameters(parameters, linkContent);
parameters.putString("message", this.getMessage());
parameters.putString("link", Utility.getUriString(linkContent.getContentUrl()));
parameters.putString("picture", Utility.getUriString(linkContent.getImageUrl()));
parameters.putString("name", linkContent.getContentTitle());
parameters.putString("description", linkContent.getContentDescription());
parameters.putString("ref", linkContent.getRef());
new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("feed"), parameters, HttpMethod.POST, requestCallback).executeAsync();
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class ShareApi method stageOpenGraphObject.
private void stageOpenGraphObject(final ShareOpenGraphObject object, final CollectionMapper.OnMapValueCompleteListener onOpenGraphObjectStagedListener) {
String type = object.getString("type");
if (type == null) {
type = object.getString("og:type");
}
if (type == null) {
onOpenGraphObjectStagedListener.onError(new FacebookException("Open Graph objects must contain a type value."));
return;
}
final JSONObject stagedObject = new JSONObject();
final CollectionMapper.Collection<String> collection = new CollectionMapper.Collection<String>() {
@Override
public Iterator<String> keyIterator() {
return object.keySet().iterator();
}
@Override
public Object get(String key) {
return object.get(key);
}
@Override
public void set(String key, Object value, CollectionMapper.OnErrorListener onErrorListener) {
try {
stagedObject.put(key, value);
} catch (final JSONException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging object.";
}
onErrorListener.onError(new FacebookException(message));
}
}
};
final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
final FacebookRequestError error = response.getError();
if (error != null) {
String message = error.getErrorMessage();
if (message == null) {
message = "Error staging Open Graph object.";
}
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, message));
return;
}
final JSONObject data = response.getJSONObject();
if (data == null) {
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, "Error staging Open Graph object."));
return;
}
final String stagedObjectId = data.optString("id");
if (stagedObjectId == null) {
onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, "Error staging Open Graph object."));
return;
}
onOpenGraphObjectStagedListener.onComplete(stagedObjectId);
}
};
final String ogType = type;
final CollectionMapper.OnMapperCompleteListener onMapperCompleteListener = new CollectionMapper.OnMapperCompleteListener() {
@Override
public void onComplete() {
final String objectString = stagedObject.toString();
final Bundle parameters = new Bundle();
parameters.putString("object", objectString);
try {
new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("objects/" + URLEncoder.encode(ogType, DEFAULT_CHARSET)), parameters, HttpMethod.POST, requestCallback).executeAsync();
} catch (final UnsupportedEncodingException ex) {
String message = ex.getLocalizedMessage();
if (message == null) {
message = "Error staging Open Graph object.";
}
onOpenGraphObjectStagedListener.onError(new FacebookException(message));
}
}
@Override
public void onError(FacebookException exception) {
onOpenGraphObjectStagedListener.onError(exception);
}
};
stageCollectionValues(collection, onMapperCompleteListener);
}
use of com.facebook.GraphRequest in project facebook-android-sdk by facebook.
the class ShareApi method shareOpenGraphContent.
private void shareOpenGraphContent(final ShareOpenGraphContent openGraphContent, final FacebookCallback<Sharer.Result> callback) {
// In order to create a new Open Graph action using a custom object that does not already
// exist (objectID or URL), you must first send a request to post the object and then
// another to post the action. If a local image is supplied with the object or action, that
// must be staged first and then referenced by the staging URL that is returned by that
// request.
final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
final JSONObject data = response.getJSONObject();
final String postId = (data == null ? null : data.optString("id"));
ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
}
};
final ShareOpenGraphAction action = openGraphContent.getAction();
final Bundle parameters = action.getBundle();
this.addCommonParameters(parameters, openGraphContent);
if (!Utility.isNullOrEmpty(this.getMessage())) {
parameters.putString("message", this.getMessage());
}
final CollectionMapper.OnMapperCompleteListener stageCallback = new CollectionMapper.OnMapperCompleteListener() {
@Override
public void onComplete() {
try {
handleImagesOnAction(parameters);
new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath(URLEncoder.encode(action.getActionType(), DEFAULT_CHARSET)), parameters, HttpMethod.POST, requestCallback).executeAsync();
} catch (final UnsupportedEncodingException ex) {
ShareInternalUtility.invokeCallbackWithException(callback, ex);
}
}
@Override
public void onError(FacebookException exception) {
ShareInternalUtility.invokeCallbackWithException(callback, exception);
}
};
this.stageOpenGraphAction(parameters, stageCallback);
}
Aggregations