Android : How to get larger profile pic from Facebook using FirebaseAuth?

后端 未结 8 563
孤街浪徒
孤街浪徒 2021-01-30 14:25

I am using FirebaseAuth to login user through FB. Here is the code:

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
pr         


        
相关标签:
8条回答
  • 2021-01-30 15:14

    If someone is looking for this but for Google account using FirebaseAuth. I have found a workaround for this. If you detail the picture URL:

    https://lh4.googleusercontent.com/../.../.../.../s96-c/photo.jpg

    The /s96-c/ specifies the image size (96x96 in this case)so you just need to replace that value with the desired size.

    String url= FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
    url = url.replace("/s96-c/","/s300-c/");
    

    You can analyze your photo URL to see if there is any other way to change its size.

    As I said in the begining, this only works for Google accounts. Check @Mathias Brandt 's answer to get a custom facebook profile picture size.

    EDIT 2020:

    Thanks to Andres SK and @alextouzel for pointing this out. Photo URLs format have changed and now you can pass URL params to get different sizes of the picture. Check https://developers.google.com/people/image-sizing.

    0 讨论(0)
  • 2021-01-30 15:15

    Note: From Graph API v8.0 you must provide the access token for every UserID request you do.

    Hitting the graph API:

    https://graph.facebook.com/<user_id>/picture?height=1000&access_token=<any_of_above_token>
    

    With firebase:

    FirebaseUser user = mAuth.getCurrentUser();
    String photoUrl = user.getPhotoUrl() + "/picture?height=1000&access_token=" +
      loginResult.getAccessToken().getToken();
    

    You get the token from registerCallback just like this

           LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                FirebaseUser user = mAuth.getCurrentUser();
                String photoUrl = user.getPhotoUrl() + "/picture?height=1000&access_token=" + loginResult.getAccessToken().getToken();
            }
    
            @Override
            public void onCancel() {
                Log.d("Fb on Login", "facebook:onCancel");
            }
    
            @Override
            public void onError(FacebookException error) {
                Log.e("Fb on Login", "facebook:onError", error);
            }
        });
    

    This is what documentation says:

    Beginning October 24, 2020, an access token will be required for all UID-based queries. If you query a UID and thus must include a token:

    • use a User access token for Facebook Login authenticated requests
    • use a Page access token for page-scoped requests
    • use an App access token for server-side requests
    • use a Client access token for mobile or web client-side requests

    We recommend that you only use a Client token if you are unable to use one of the other token types.

    0 讨论(0)
提交回复
热议问题