Using facebook graph api to retrieve feeds of a page

时间:2015-05-24 20:43:13

标签: java android facebook facebook-graph-api mobile

I am developing an application for a customer, and I have read in app (android platform) posts which he (customer) publishes on a fan page.

I created the method below and when I use the keyword /me runs and got the output to my personal information, but when I use the keyword "729745317037227/feed/" the app closes and returns nothing.

                        new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "729745317037227/feed/",
                            null,
                            HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    Toast.makeText(
                                            getApplicationContext(),
                                            response.getJSONObject()
                                                    + "",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                    ).executeAsync();

The entire code, I'm putting in the main, for now I'm just trying to make queries and display the results for later use in fragments.

Main class

package com.clubee.marketnow;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.Profile;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class MainActivity extends Activity {

private TextView mainTextView;
private static final int request_code = 5;
public CallbackManager mCallbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
    mCallbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    mainTextView = (TextView) findViewById(R.id.lol);
    loginButton.setReadPermissions("user_friends");
    LoginManager.getInstance().logOut(); //ensures that whenever we start the app we're logged out

    LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Profile curProfile = Profile.getCurrentProfile();
                    startNewIntent(curProfile);

                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "729745317037227/feed/",
                            null,
                            HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    Toast.makeText(
                                            getApplicationContext(),
                                            response.getJSONObject()
                                                    + "",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                    ).executeAsync();

                }

                @Override
                public void onCancel() {
                    // App code
                    mainTextView.setText("Status: Logging in cancelled");
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    mainTextView.setText("Status: EXCEPTION: " + exception.toString());
                }
            });
}

private void startNewIntent(Profile elUsero){
    Intent i = new Intent(this, RetornoFacebook.class);

    i.putExtra("firstName", elUsero.getFirstName());
    i.putExtra("lastName", elUsero.getLastName());
    i.putExtra("picURI", elUsero.getProfilePictureUri(1500,1500).toString());
    startActivityForResult(i, request_code);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

@Override
protected void onPause() {
    super.onPause();
    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mCallbackManager.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == request_code) && (resultCode == RESULT_OK)) {
        LoginManager.getInstance().logOut();
        mainTextView.setText("Status: Logged Out!");
    }
}
}

Can anyone help me understand how I do to return the feed of a fan page? I am racking my brain for weeks with this and do not know where I am going wrong. I have read a million times the fb documentation, and the max I got it the current result.

PS: When I do research in graph api explorer, using the same key (729745317037227/feed/), the return is a success.

tks a lot

1 个答案:

答案 0 :(得分:0)

问题可能是因为您使用的access token类型。

  1. &#34; /我运行并输出到我的个人信息&#34;:调用此边缘需要具有相应权限的用户访问令牌。

  2. &#34;当我使用关键字&#34; 729745317037227 / feed /&#34;应用程序关闭并且不返回任何内容&#34;:调用此边缘需要具有适当权限的页面访问令牌。您可能在此处使用用户访问令牌。

  3.   

    页面访问令牌 - 这些访问令牌与用户访问类似   令牌,除了它们为读取,写入的API提供权限   或修改属于Facebook页面的数据。