Pregunta

Hola chicos, puedo iniciar sesión al usuario en su cuenta de Facebook y publicar un contenido en su perfil. Pero lo que no puedo hacer es obtener información de los usuarios de Facebook. No sé dónde está el token de acceso que si lo da Facebook para una sesión. Estoy usando fbconnect.jar y la agrego en mi carpeta lib y la ruta de compilación que sigue es el código que estoy usando:-

private Facebook facebookClient;
String information;
Button fb;
//** Called when the activity is first created. *//*
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fb = (Button)findViewById(R.id.fb);
    fb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        whenFbPressed();

        }
    });



}


@Override
public void onCancel() {
    // TODO Auto-generated method stub

}

@Override
public void onComplete(Bundle values) {
    // TODO Auto-generated method stub

    if (values.isEmpty()) {
        return;
    }

    if (!values.containsKey("post_id")) {
        try {
            Bundle parameters = new Bundle();
            parameters
                    .putString("message", "is Listening to  " );// the

              parameters.putString("attachment",
              "{\"name\":\"My Test Image\"," + "\"href\":\"" +
              "http://www.google.com" + "\"," +
              "\"media\":[{\"type\":\"image\",\"src\":\"" +
              "http://www.google.com/logos/mucha10-hp.jpg" +
              "\",\"href\":\"" + "http://www.google.com" + "\"}]" + "}");

            parameters.putString("attachment", "{\"name\":\"" + "Abhishek"
                    + "\"," + "\"href\":\"" + "www.google.com" + "\"}]" + "}");

            facebookClient.dialog(this, "stream.publish", parameters, this);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

@Override
public void onError(DialogError e) {
    // TODO Auto-generated method stub
    System.out.println("Error: " + e.getMessage());
}

@Override
public void onFacebookError(FacebookError e) {
    // TODO Auto-generated method stub
    System.out.println("Error: " + e.getMessage());

}

public void onClick(View v) {
    if (v == fb) {
        facebookClient = new Facebook("App_ID");
        // facebookClient = new Facebook("App_ID");
        // replace APP_API_ID with your own
        facebookClient.authorize(this, new String[] { "publish_stream",
                "read_stream", "offline_access" }, this);
    }
}

public void whenFbPressed(){
    facebookClient = new Facebook("App_ID");
    // facebookClient = new Facebook("App_ID");
    // replace APP_API_ID with your own
    facebookClient.authorize(this, new String[] { "publish_stream",
            "read_stream", "offline_access","email" }, this);

}
¿Fue útil?

Solución

/* this is user defined method  to fetch current user firstnam and last name */

public void getUser(){

    try{
        JSONObject json = Util.parseJson( mFacebook.request("me"));
        String facebookID = json.getString("id");
        String firstName = json.getString("first_name");
        String lastName = json.getString("last_name");
        mFirstName=firstName;
        mLastName=lastName;
        mText.setText("You are logged in : "+mFirstName+"."+mLastName);
        System.out.println("Firstname>>"+firstName+" LastName>>"+lastName);
    }catch (Exception e) {
        // TODO: handle exception
    }catch(FacebookError fbe){

    }
}

Otros consejos

Bueno, lo tengo.

Facebook envía el token de acceso cuando el usuario registra la aplicación y permite los permisos dados.

Entonces en mi onComplete() Método Agregué este Peice of Code:

if (values.isEmpty()) {
     return;
}
if (!values.containsKey("post_id")) {
     try {
          Log.d("++++++++++++", facebookClient.getAccessToken());
          Log.d("++++++++++++", facebookClient.request("me"));
     }
}

Y en mi registro me da datos.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top