Click here to Skip to main content
15,887,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to open the Login Authorization page in facebook app without Facebook SDK. I tried the method from here but it opens the login page in the browser/WebView.I also tried this method from here but I am getting an error with it ('the parameter redirect_uri is required') even though my redirect_uri works fine. I have tested it. Is there a way to open Login page within the Facebook app installed on the device? My code is below:

What I have tried:

public class MainActivity extends AppCompatActivity {


private final String clientId = "XXXXXXXXXXXXXXXX";
private final String responseType = "code";

private final String redirectUri = "https://waheedabbax.app.link/3LJBUFVfqP";
public String FACEBOOK_PAGE_ID = "https://www.facebook.com/v3.1" + "/dialog/oauth" +
        "?client_id=" + clientId  +
        "&redirect_uri=" + redirectUri +
        "&response_type=" + responseType;

public String FACEBOOK_URL = "https://www.facebook.com/v3.1" + "/dialog/oauth" +
        "?client_id=" + clientId +
        "&redirect_uri=" + redirectUri +
        "&response_type=" + responseType;

/**
 * same as in manifest in intent filter
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Initialize the Branch object
    Branch.getAutoInstance(this);

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.waheed.facebooklogin",  // replace with your unique package name
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    Button loginButton = (Button) findViewById(R.id.loginbutton);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              /*  Intent intent = new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("https://m.facebook.com/v3.1" + "/dialog/oauth" +
                                "?client_id=" + clientId + "&display=popup"+
                                "&redirect_uri=" + redirectUri +
                                "&response_type=" + responseType));
                startActivity(intent);*/
            Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
            String facebookUrl = getFacebookPageURL(getApplicationContext());
            Log.d("com.waheed",facebookUrl);
            facebookIntent.setData(Uri.parse(facebookUrl));
            startActivity(facebookIntent);

        }
    });
}

@Override
protected void onResume() {
    super.onResume();

    // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
    Uri uri = getIntent().getData();
    if (uri != null && uri.toString().startsWith(redirectUri)) {
        // use the parameter your API exposes for the code (mostly it's "code")
        String code = uri.getQueryParameter("code");
        if (code != null) {
            Log.i("code", code);
            // get access token
            // we'll do that in a minute
        } else if (uri.getQueryParameter("error") != null) {
            // show an error message here
        }
    }
}

//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
        if (versionCode >= 3002850) { //newer versions of fb app
            return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
        } else { //older versions of fb app
            return "fb://page/" + FACEBOOK_PAGE_ID;
        }
    } catch (PackageManager.NameNotFoundException e) {
        return FACEBOOK_URL; //normal web url
    }
}
Posted
Updated 16-Aug-18 23:31pm
v2
Comments
[no name] 17-Aug-18 1:47am    
"I don't want to use the SDK"?

No one has time for that.
Member 13951757 17-Aug-18 3:07am    
Facebook SDK increases the size of my app that is why I am avoiding it.
Richard MacCutchan 17-Aug-18 6:25am    
Use the Facebook SDK, that is what it was created for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900