Integrate Instagram Authentication in Android Application

Posted By : Keshav Gupta | 18-Jan-2018

Server side setup : 

1.Create a record at Instagram developer url and enlist application for sandbox account.Following is url: 

https://www.instagram.com/developer/ 

2. Get clientId and clientSecret key.You need to utilize them in application side.Make beyond any doubt that your callbackUrl in application and redirectUrl ought to be same. 

Android Application Code :

1.Create a class named as AppConstants where we will put above generated credentials.Code will be like:

public static String INSTAGRAM_CALLBACK_URL = "Your callback url";
   // replace below credentials with yours own
    public static String INSTAGRAM_CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static String INSTAGRAM_CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String INSTAGRAM_ACCESS_TOKEN = "INSTAGRAM_ACCESS_TOKEN";

    public static boolean isInternetIsAvailable(Context mContext) {
        ConnectivityManager connectivity = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }

2.Make SharedPrefernce class to hold access token locally in device.Create another class named as SessionManager andCode it like this:

  private static final String prefsfile = "MyPrefsName";
    SharedPreferences preferences;
    SharedPreferences.Editor editor;
    Context context;

    public SessionManager(Context context) {
        this.context = context;
        preferences = context.getSharedPreferences(prefsfile,
                Context.MODE_PRIVATE);
        editor = preferences.edit();
    }
    public void saveInstagramAccessToken(String stringToken)
    {
        editor.putString(AppConstants.INSTAGRAM_ACCESS_TOKEN,stringToken);
        editor.commit();
    }

    public String getInstagramAccessToken() {
        return preferences.getString(AppConstants.INSTAGRAM_ACCESS_TOKEN,"");
    }

3.Now we will create a Instagram Configuration class which will generate authUrl ,tokenUrl and will  get a call back for AuthDialogListener which will further generate success and failure responses.Code will be like this:

4.Now we will create  a InstagramDialog class which will represent authentication window where user will login to its instagram account.This class will load a webview for authurl for instagram.Code will be like this:

6.Create a layout xml file containing Button and pass its reference in activity class as above.

public class InstagramActivity extends AppCompatActivity {
    private Button button;
    private Context context;
    private InstagramConfigApp instagramConfigApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instagram);
        context = InstagramActivity.this;
        button = findViewById(R.id.btnInstagram);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (AppConstants.isInternetIsAvailable(context)) {
                    instagramConfigApp =new InstagramConfigApp(context,AppConstants.INSTAGRAM_CLIENT_ID,AppConstants.INSTAGRAM_CLIENT_SECRET,
                            AppConstants.INSTAGRAM_CALLBACK_URL);
                    if (instagramConfigApp.hasAccessToken()) {
                        instagramConfigApp.authorize();
                    }
                } else {
                    Toast.makeText(context, "Oops!Connection suspended.Check your internet connection", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

7.That's all you have to do.

8.For referrence :

You can refer these: instagram developer site

 

 

 

 

About Author

Author Image
Keshav Gupta

Keshav Gupta is Android Developer in Oodles, he always look forward for new tasks and new things to learn more.

Request for Proposal

Name is required

Comment is required

Sending message..