Search in sources :

Example 6 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class DeviceAuthDialog method onSuccess.

private void onSuccess(final String accessToken) {
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,permissions,name");
    AccessToken temporaryToken = new AccessToken(accessToken, FacebookSdk.getApplicationId(), "0", null, null, null, null, null);
    GraphRequest request = new GraphRequest(temporaryToken, "me", parameters, HttpMethod.GET, new GraphRequest.Callback() {

        @Override
        public void onCompleted(GraphResponse response) {
            if (completed.get()) {
                return;
            }
            if (response.getError() != null) {
                onError(response.getError().getException());
                return;
            }
            String userId;
            Utility.PermissionsPair permissions;
            String name;
            try {
                JSONObject jsonObject = response.getJSONObject();
                userId = jsonObject.getString("id");
                permissions = Utility.handlePermissionResponse(jsonObject);
                name = jsonObject.getString("name");
            } catch (JSONException ex) {
                onError(new FacebookException(ex));
                return;
            }
            DeviceRequestsHelper.cleanUpAdvertisementService(currentRequestState.getUserCode());
            boolean requireConfirm = FetchedAppSettingsManager.getAppSettingsWithoutQuery(FacebookSdk.getApplicationId()).getSmartLoginOptions().contains(SmartLoginOption.RequireConfirm);
            if (requireConfirm && !isRetry) {
                isRetry = true;
                presentConfirmation(userId, permissions, accessToken, name);
                return;
            }
            completeLogin(userId, permissions, accessToken);
        }
    });
    request.executeAsync();
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) AccessToken(com.facebook.AccessToken) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 7 with GraphResponse

use of com.facebook.GraphResponse 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();
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle) JSONException(org.json.JSONException) FacebookRequestError(com.facebook.FacebookRequestError)

Example 8 with GraphResponse

use of com.facebook.GraphResponse in project facebook-android-sdk by facebook.

the class ShareApi method stagePhoto.

private void stagePhoto(final SharePhoto photo, final CollectionMapper.OnMapValueCompleteListener onPhotoStagedListener) {
    final Bitmap bitmap = photo.getBitmap();
    final Uri imageUrl = photo.getImageUrl();
    if ((bitmap != null) || (imageUrl != null)) {
        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 photo.";
                    }
                    onPhotoStagedListener.onError(new FacebookGraphResponseException(response, message));
                    return;
                }
                final JSONObject data = response.getJSONObject();
                if (data == null) {
                    onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
                    return;
                }
                final String stagedImageUri = data.optString("uri");
                if (stagedImageUri == null) {
                    onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
                    return;
                }
                final JSONObject stagedObject = new JSONObject();
                try {
                    stagedObject.put("url", stagedImageUri);
                    stagedObject.put("user_generated", photo.getUserGenerated());
                } catch (final JSONException ex) {
                    String message = ex.getLocalizedMessage();
                    if (message == null) {
                        message = "Error staging photo.";
                    }
                    onPhotoStagedListener.onError(new FacebookException(message));
                    return;
                }
                onPhotoStagedListener.onComplete(stagedObject);
            }
        };
        if (bitmap != null) {
            ShareInternalUtility.newUploadStagingResourceWithImageRequest(AccessToken.getCurrentAccessToken(), bitmap, requestCallback).executeAsync();
        } else {
            try {
                ShareInternalUtility.newUploadStagingResourceWithImageRequest(AccessToken.getCurrentAccessToken(), imageUrl, requestCallback).executeAsync();
            } catch (final FileNotFoundException ex) {
                String message = ex.getLocalizedMessage();
                if (message == null) {
                    message = "Error staging photo.";
                }
                onPhotoStagedListener.onError(new FacebookException(message));
            }
        }
    } else {
        onPhotoStagedListener.onError(new FacebookException("Photos must have an imageURL or bitmap."));
    }
}
Also used : GraphRequest(com.facebook.GraphRequest) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) Uri(android.net.Uri) FacebookRequestError(com.facebook.FacebookRequestError) Bitmap(android.graphics.Bitmap) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) FacebookGraphResponseException(com.facebook.FacebookGraphResponseException)

Example 9 with GraphResponse

use of com.facebook.GraphResponse 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();
}
Also used : FacebookCallback(com.facebook.FacebookCallback) GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) Bundle(android.os.Bundle)

Example 10 with GraphResponse

use of com.facebook.GraphResponse 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);
}
Also used : GraphRequest(com.facebook.GraphRequest) Bundle(android.os.Bundle) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FacebookRequestError(com.facebook.FacebookRequestError) FacebookCallback(com.facebook.FacebookCallback) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) FacebookException(com.facebook.FacebookException) CollectionMapper(com.facebook.internal.CollectionMapper) JSONObject(org.json.JSONObject) FacebookGraphResponseException(com.facebook.FacebookGraphResponseException)

Aggregations

GraphRequest (com.facebook.GraphRequest)19 GraphResponse (com.facebook.GraphResponse)19 JSONObject (org.json.JSONObject)16 Bundle (android.os.Bundle)15 JSONException (org.json.JSONException)11 FacebookException (com.facebook.FacebookException)8 FacebookCallback (com.facebook.FacebookCallback)6 FacebookRequestError (com.facebook.FacebookRequestError)6 AccessToken (com.facebook.AccessToken)4 Uri (android.net.Uri)3 Bitmap (android.graphics.Bitmap)2 FacebookGraphResponseException (com.facebook.FacebookGraphResponseException)2 CollectionMapper (com.facebook.internal.CollectionMapper)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Intent (android.content.Intent)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 AppLink (bolts.AppLink)1