怎么能在没有检查的情况下从带有API墙的Android应用程序在Facebook上发布?

时间:2013-07-02 13:20:50

标签: android facebook facebook-android-sdk android-facebook

如何在没有检查的情况下从Android应用程序使用API​​墙在Facebook上发布(从Facebook显示“post to wall”屏幕)

3 个答案:

答案 0 :(得分:4)

致电publishStory(StringYouNeed,StringYouNeed,StringYouNeed); 并在您的活动/片段中实现:

private void publishStory(String hash, String title, String user) {

    Session session = Session.getActiveSession();

    if (session != null){
        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(getActivity(), PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }
        Bundle postParams = new Bundle();
        postParams.putString("name", title);
        postParams.putString("caption", "bla bla");
        postParams.putString("description", "bla bla");
        postParams.putString("link", "http://blabla.com/"+hash);

        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(TAG,
                            "JSON error "+ e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    debug.print("erreur");
                } 
            }
        };

        Request request = new Request(session, "me/feed", postParams, 
                HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }

}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

对我来说很完美,希望有这个帮助。

答案 1 :(得分:1)

答案 2 :(得分:0)

创建您的网页并提供链接

// use this function
private void shareToFacebook() {

    // add urlToShare to your own site url
    String urlToShare = "http://mithsoft.net/home/";
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);

   // See if official Facebook app is found
    boolean facebookAppFound = false;
    List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
            intent.setPackage(info.activityInfo.packageName);
            facebookAppFound = true;
            break;
        }
    }
   // As fallback, launch sharer.php in a browser
    if (!facebookAppFound) {
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
    }
    startActivity(intent);
}