FaceBook第三方登录获取姓名,头像,邮箱等个人信息

最近项目新增一个需求:需要收集用户的邮箱信息。但是直接通过GraphRequest.newMeRequest去获取发现返回值为空,通过查阅资料得知facebook获取用户邮箱还需要授权。

授权方式如下

  LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile,email"));

获取姓名邮箱信息监听事件如下

 GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                if (object == null) {
                    mPresenter.loginThirdPlat(2, 			                  loginResult.getAccessToken().getUserId(), nation, information.email);
                    return;
                }
                String id = object.optString("id");  
                String name = object.optString("name"); 
                String gender = object.optString("gender"); 
                String email = object.optString("email"); 
                String locale = object.optString("locale");  
                //获取用户头像
                 JSONObject object_pic = object.optJSONObject("picture");
             	 JSONObject object_data = object_pic.optJSONObject("data");
                String photo = object_data.optString("url");
             
                getFacebookUserPictureAsync(id, new FaceUserImgCallBack() {
                    @Override
                    public void onFailed(String msg) {
                        LogUtil.v("fb", msg)
                    }

                    @Override
                    public void onCompleted(String url) {
                        LogUtil.v("fb", url);
                    }
                });
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,link,gender,birthday,picture,locale,updated_time,timezone,age_range,first_name,last_name");

        request.setParameters(parameters);
        request.executeAsync();
       

注意需要先注册监听事件,然后需要获取信息时再去执行授权代。

通过上述方式可以获取到头像信息,不过给的是默认大小的图片地址,如果需要自定义大小的头像地址需要通过如下方式去获取:

  /**
   * 通过facebook的userId,联网异步获取用户的头像url
   *
   * @param facebookUserId
   */
  public static void getFacebookUserPictureAsync(String facebookUserId, FaceUserImgCallBack callback) {
      LogUtil.i("fb", "准备获取用户头像");
      if (facebookUserId == null || facebookUserId.length() == 0) return;
      Bundle parameters = new Bundle();
      parameters.putBoolean("redirect", false);
      parameters.putString("height", "500");
      parameters.putString("type", "normal");
      parameters.putString("width", "500");
      GraphRequest graphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + facebookUserId + "/picture", parameters, HttpMethod.GET, new GraphRequest.Callback() {
          public void onCompleted(GraphResponse response) {
              if (response == null) {
                  callback.onFailed("facebook failed");
                  return;
              }
              if (response.getError() != null) {
                  FacebookRequestError facebookRequestError = response.getError();
                  callback.onFailed("facebook failed:" + facebookRequestError.getErrorMessage());
                  return;
              }
              JSONObject responseJsonObject = response.getJSONObject();
              if (responseJsonObject == null) {
                  callback.onFailed("facebook failed");
                  return;
              }
              LogUtil.i("fb", "facebook直接返回的头像信息是" + responseJsonObject.toString());
              String avatarUrl = "";
              try {
                  JSONObject dataJsonObject = responseJsonObject.getJSONObject("data");
                  avatarUrl = dataJsonObject.getString("url");
//                    avatarUrl = URLEncoder.encode(avatarUrl, "UTF-8");
                  LogUtil.i("fb", "用户头像获取完毕 avatarUrl:" + avatarUrl);
              } catch (Exception e) {
                  callback.onFailed("facebook failed" + e.getStackTrace().toString());
              }
              callback.onCompleted(avatarUrl);
          }
      }
      );
      LogUtil.i("fb", "version:" + graphRequest.getVersion() + "");
      graphRequest.executeAsync();
  }

  public interface FaceUserImgCallBack {
      void onFailed(String msg);

      void onCompleted(String url);
  }

如此便可以轻松获取facebook相关个人信息

你可能感兴趣的:(java,android)