获取 Facebook 用户数据

发布于 2024-10-26 02:53:38 字数 2663 浏览 1 评论 0原文

嘿伙计们 我能够将用户登录到其 Facebook 帐户并在他的个人资料上发布内容。 但我无法做的是获取 Facebook 用户信息。 我不知道 facebook 为会话提供的访问令牌在哪里。 我正在使用 fbConnect.jar 并将其添加到我的 lib 文件夹和构建路径中 以下是我正在使用的代码:-

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);

}

Hey guys
I am able to login the user to its facebook account and post a content on his profile.
But what i am not able to do is get facebook users information.
I dont know where is the access token which if given by facebook for a session.
I am using fbConnect.jar and adding it in my lib folder and build path
Following is the code i am using :-

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-11-02 02:53:38
/* 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){

    }
}
/* 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){

    }
}
已下线请稍等 2024-11-02 02:53:38

好吧,我明白了。

当用户注册应用程序并允许给定权限时,Facebook 会发送访问令牌。

因此,在我的 onComplete() 方法中,我添加了这段代码:

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

并在我的日志中提供了数据。

Well i got it.

Facebook sends the access token when the user registers the application and allows the given permissions.

So in my onComplete() method i added this peice of code :

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

and in my log it gives me data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文