我在Android上用“登录facebook”创建了一个简单的测试App。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
String email;
String birthday;
String name;
String gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
info = (TextView)findViewById(R.id.fb_text);
loginButton = (LoginButton)findViewById(R.id.fb_login);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
// Application code
try {
email = object.getString("email");
birthday = object.getString("birthday"); // 01/31/1980 format
name = object.getString("name");
gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
}
info.append(
"User Name: " + name
+ "\n" +
"birthday: "
+ birthday
+ "\n" +
"Gender: "
+ gender
+ "\n" +
"Email: "
+ email
);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
info.setText("Login Cancelled");
}
@Override
public void onError(FacebookException e) {
info.setText("Login Failed");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
如何获取用户的个人资料照片并将其显示在我的活动中?
答案 0 :(得分:6)
只需在现有代码中添加以下参数
即可parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)");
picture.type(large)将返回带有您个人资料照片的网址
顺便说一下,你可以从facebook返回的JSONObject获取该URL,如下所示:
profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
答案 1 :(得分:3)
通过
获取loggedIn用户的facebook_idpublic static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var cors = new EnableCorsAttribute("http://example.net", "*", "*");
config.EnableCors(cors);
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
第一种方法
使用name = object.getString("name");
facebook_id = object.getString("id");
代替ProfilePictureView
ImageView
并使用facebook_id将ProfilePic设置为此行
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true" />
第二种方法
使用facebook_id获取用户个人资料照片的位图,并将此位图设置为ImageView。
profilePictureView.setProfileId(facebook_id);
答案 2 :(得分:2)
尝试这个获取fb用户的ID,然后通过该ID获取配置文件。
GraphRequest request = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
try {
Logs.log(TAG, response.getJSONObject().toString());
String id = response.getJSONObject().getString("id");
String email = response.getJSONObject().getString("email");
String name = response.getJSONObject().getString("name");
} catch (JSONException e) {
Logs.log(e);
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
然后用户的个人资料照片将位于: -
"https://graph.facebook.com/"+id of user+"/picture?type=large"