Monday 20 April 2015

Login Via Instagram and getting user profile info Android

Generally we get an requirement to login via instagram in our android app. So how to do the i'm going to describe here :


Download and include instagramlib.jar in libs

Follow the code and you are done :

1. MainActivity.java

public class MainActivity extends Activity {
private InstagramApp mApp;
private Button btnConnect,btnshowpofile;
private TextView tvSummary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instagram_main);
mApp = new InstagramApp(this, ApplicationData.CLIENT_ID,
ApplicationData.CLIENT_SECRET, ApplicationData.CALLBACK_URL);
mApp.setListener(listener);
tvSummary = (TextView) findViewById(R.id.tvSummary);
btnConnect = (Button) findViewById(R.id.btnConnect);
btnshowpofile = (Button) findViewById(R.id.btnshowpofile);
btnshowpofile.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openInstagram(MainActivity.this, mApp.getUserName());
}
}); 
btnConnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) 
{
if (mApp.hasAccessToken()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Disconnect from Instagram?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
mApp.resetAccessToken();
btnConnect.setText("Connect");
tvSummary.setText("Not connected");
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} 
else 
{
mApp.authorize();
}
}
});
if (mApp.hasAccessToken()) 
{
tvSummary.setText("Connected as " + mApp.getUserName());
btnConnect.setText("Disconnect");
}
}

OAuthAuthenticationListener listener = new OAuthAuthenticationListener() {
@Override
public void onSuccess() {
tvSummary.setText("Connected as " + mApp.getUserName());
btnConnect.setText("Disconnect");
}
@Override
public void onFail(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
}
};
private void openInstagram(Context con,String mUserName) {
 try {
  Intent iIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
  iIntent.setData(Uri.parse("instagram://user?username="+mUserName));   
  con.startActivity(iIntent);
 } 
catch (Exception e) {
  con.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/"+mUserName)));
}
}
}

Note : get your scerets keys from instagram developer when Registering your app.

2. Instragramkeys.java

public class Instragramkeys{
public static final String CLIENT_ID = "your client id here";
public static final String CLIENT_SECRET = "your client secret id here";
public static final String CALLBACK_URL = "instagram://connect";
}

XML Layout:

1. instagram_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnConnect"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="40dip"
        android:text="Connect" />

    <Button
        android:id="@+id/btnshowpofile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="40dip"
        android:text="showprofile" />

    <TextView
        android:id="@+id/tvSummary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_marginLeft="15dip"
        android:layout_marginTop="5dp"
        android:text="Not connected" />

</LinearLayout>



2. mainifestfile

  <uses-permission android:name="android.permission.INTERNET" />

output Screens :







2 comments:

  1. Binance clone script is a well-designed, built, and extensively tested website clone script that is ready to launch the world's most popular cryptocurrency exchange. This script incorporates all of Binance's existing features (Admin, User, and Security). Are you seeking software development services to build an exceptional Binance-like cryptocurrency exchange platform? Get industry-standard features like market order types, live trading charts, and more.

    ReplyDelete
  2. Omninos offers Binance Clone Script to create a popular crypto exchange like Binance. Our White Label Binance Clone Software helps you build a similar exchange website like Binance.

    Omninos is a well known cryptocurrency exchange development company with prior experience in crafting binance like exchange instantly for your crypto business. Get a Free Demo of Binance Clone Script!!

    ReplyDelete