利用facebook api进行登录,并调用api

1.先在facebook官网上下载
facebook-facebook-android-sdk-v1.2.1-0-g5a72863.zip(下载地址 : https://github.com/facebook/facebook-android-sdk)
2. new --Android Project -----create project from existing source ----选择facebook ,

3.创建一个新的应用,要引用 facebook项目,方法(右键---properties---Android----Add library 选择facebook项目)

4.在facebook平台上创建应用 ,获得APP_ID。
验证 登录的代码


public static final String APP_ID = "374076005970091";
private Facebook mFacebook= new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner= new AsyncFacebookRunner(mFacebook);
private static final String[] PERMISSIONS =
new String[] {"publish_stream", "read_stream", "offline_access"};

public void onClick(View v) {
// TODO Auto-generated method stu
mFacebook.authorize(this,
PERMISSIONS, new LoginDialogListener());

}

获得个人信息的代码:

mAsyncRunner.request("me", new SelfListener());
class SelfListener implements com.facebook.android.AsyncFacebookRunner.RequestListener{

@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FaceBookTestActivity.this, ResultActivity.class);
//response是个人信息的json字符串
intent.putExtra("response", response);
startActivity(intent);
}

发表说说的代码:
mFacebook.dialog(ResultActivity.this, "stream.publish",
new WallPostDialogListener());

public class WallPostDialogListener implements
com.facebook.android.Facebook.DialogListener {

/**
* Called when the dialog has completed successfully
*/
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("FB Sample App", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
} else {
Log.d("FB Sample App", "No wall post made");
}
}

@Override
public void onCancel() {
// No special processing if dialog has been canceled
}

@Override
public void onError(DialogError e) {
// No special processing if dialog has been canceled
}

@Override
public void onFacebookError(FacebookError e) {
// No special processing if dialog has been canceled
}
}

public class WallPostRequestListener implements
com.facebook.android.AsyncFacebookRunner.RequestListener {

/**
* Called when the wall post request has completed
*/

@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.d("Facebook-Example", "Got response: " + response);
}

@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub

}

@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub

}

@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub

}

@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub

}

}

你可能感兴趣的:(利用facebook api进行登录,并调用api)