美文网首页
FaceBook 登陆后获取图片

FaceBook 登陆后获取图片

作者: coffeeteas | 来源:发表于2018-10-30 14:55 被阅读15次

    https://cloud.tencent.com/developer/ask/92433

    使用新的API和您的Facebook的自定义按钮,你可以使用以下代码:

    在你的gradle文件中放置下面的gradle:

    compile'com.facebook.android:facebook-android-sdk:4.20.0'Newer sdk does not need initializaion.privateCallbackManager callbackManager;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);FacebookSdk.sdkInitialize(LoginActivity.this);//Is now depricatedsetContentView(R.layout.activity_login);callbackManager=CallbackManager.Factory.create();}

    onActivityResult:

    @OverrideprotectedvoidonActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode,resultCode,data);callbackManager.onActivityResult(requestCode,resultCode,data);}

    按钮点击:

    @OverridepublicvoidonClick(View v){switch(v.getId()){caseR.id.btn_f_sign_in_login:LoginManager.getInstance().logInWithReadPermissions(this,Arrays.asList("user_friends","email","public_profile"));LoginManager.getInstance().registerCallback(callbackManager,newFacebookCallback<LoginResult>(){@OverridepublicvoidonSuccess(LoginResult loginResult){setFacebookData(loginResult);}@OverridepublicvoidonCancel(){}@OverridepublicvoidonError(FacebookException exception){}});break;}}

    setFacebookData:

    privatevoidsetFacebookData(final LoginResult loginResult){GraphRequest request=GraphRequest.newMeRequest(loginResult.getAccessToken(),newGraphRequest.GraphJSONObjectCallback(){@OverridepublicvoidonCompleted(JSONObject object,GraphResponse response){// Application codetry{Log.i("Response",response.toString());String email=response.getJSONObject().getString("email");String firstName=response.getJSONObject().getString("first_name");String lastName=response.getJSONObject().getString("last_name");String gender=response.getJSONObject().getString("gender");Profile profile=Profile.getCurrentProfile();String id=profile.getId();String link=profile.getLinkUri().toString();Log.i("Link",link);if(Profile.getCurrentProfile()!=null){Log.i("Login","ProfilePic"+Profile.getCurrentProfile().getProfilePictureUri(200,200));}Log.i("Login"+"Email",email);Log.i("Login"+"FirstName",firstName);Log.i("Login"+"LastName",lastName);Log.i("Login"+"Gender",gender);}catch(JSONExceptione){e.printStackTrace();}}});Bundle parameters=newBundle();parameters.putString("fields","id,email,first_name,last_name,gender");request.setParameters(parameters);request.executeAsync();}

    获取Facebook朋友谁下载了你的应用程序:

    替换parameters.putString(“fields”,“id,email,first_name,last_name”);

    带有parameters.putString(“fields”,“id,email,first_name,last_name,friends”);

    添加以下逻辑来获取朋友数据:

    if(object.has("friends")){JSONObject friend=object.getJSONObject("friends");JSONArray data=friend.getJSONArray("data");for(int i=0;i<data.length();i++){Log.i("idddd",data.getJSONObject(i).getString("id"));}}

    相关文章

      网友评论

          本文标题:FaceBook 登陆后获取图片

          本文链接:https://www.haomeiwen.com/subject/wnegtqtx.html