Code Monkey home page Code Monkey logo

instagramapi's Introduction

Instagram API

A wrapper on instagram API for android.

Download Android Arsenal

Download

####Gradle:

maven { url 'https://dl.bintray.com/sayyam/maven' }

compile 'com.github.sayyam:instagramapi:0.0.6'

####Maven:

<dependency>
  <groupId>com.github.sayyam</groupId>
  <artifactId>instagramapi</artifactId>
  <version>0.0.6</version>
  <type>pom</type>
</dependency>

Usage

####Include Application's Client Id and Redirect Uri: Add following meta tags in <application> tag of your AndroidManifest.

  <meta-data
    android:name="com.instagram.instagramapi.InstagramAppClientId"
    android:value="PUT-YOUR-CLIENT-ID-HERE" />

  <meta-data
    android:name="com.instagram.instagramapi.InstagramAppRedirectURL"
    android:value="PUT-YOUR-REDIRECT-URI-HERE" />

You can get above credentials from Instagram Developrs Portal.

####Initiate the authorization process: You can start the InstagramEngine in two different ways:

  1. Intent
  2. InstagramLoginButton

####1- Initiate the authorization process via Intent:

Create an intent
  String[] scopes = {InstagramKitLoginScope.BASIC, InstagramKitLoginScope.COMMENTS};
  
  Intent intent = new Intent(SampleActivity.this, InstagramAuthActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
          Intent.FLAG_ACTIVITY_SINGLE_TOP);

  intent.putExtra(InstagramEngine.TYPE, InstagramEngine.TYPE_LOGIN);
  //add scopes if you want to have more than basic access
  intent.putExtra(InstagramEngine.SCOPE, scopes);
  
  startActivityForResult(intent, 0);
Handle Login Result
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);

      switch (requestCode) {
          case 0:

              if (resultCode == RESULT_OK) {

                  Bundle bundle = data.getExtras();

                  if (bundle.containsKey(InstagramKitConstants.kSessionKey)) {

                      IGSession session = (IGSession) bundle.getSerializable(InstagramKitConstants.kSessionKey);

                      Toast.makeText(SampleActivity.this, "Woohooo!!! User trusts you :) " + session.getAccessToken(),
                              Toast.LENGTH_LONG).show();
                  }
              }
              break;
      }

  }

####2- Initiate the login process via InstagramLoginButton:

#####Add InstagramLoginButton in your layout

  <com.instagram.instagramapi.widgets.InstagramLoginButton
      android:id="@+id/instagramLoginButton"
      android:layout_width="200dp"
      android:layout_height="55dp"
      android:text="Login Button" />

#####Initialize InstagramLoginButton in your Activity

  String[] scopes = {InstagramKitLoginScope.BASIC, InstagramKitLoginScope.COMMENTS};
    
 InstagramLoginButton instagramLoginButton = (InstagramLoginButton) findViewById(R.id.instagramLoginButton);
 instagramLoginButton.setInstagramLoginCallback(instagramLoginCallbackListener);
 //if you dont specify scopes, you will have basic access.
 instagramLoginButton.setScopes(scopes);

#####Handle Login callback

  InstagramLoginCallbackListener instagramLoginCallbackListener = new InstagramLoginCallbackListener() {
      @Override
      public void onSuccess(IGSession session) {

          Toast.makeText(SampleActivity.this, "Wow!!! User trusts you :) " + session.getAccessToken(),
                  Toast.LENGTH_LONG).show();

      }

      @Override
      public void onCancel() {
          Toast.makeText(SampleActivity.this, "Oh Crap!!! Canceled.",
                  Toast.LENGTH_LONG).show();

      }

      @Override
      public void onError(InstagramException error) {
          Toast.makeText(SampleActivity.this, "User does not trust you :(\n " + error.getMessage(),
                  Toast.LENGTH_LONG).show();

      }
  };

Fetch User details:

#####Get user details via getUserDetails()

InstagramEngine.getInstance(SampleActivity.this).getUserDetails(instagramUserResponseCallback);

#####Handle Response in callback

  InstagramAPIResponseCallback<IGUser> instagramUserResponseCallback = new InstagramAPIResponseCallback<IGUser>() {
      @Override
      public void onResponse(IGUser responseObject, IGPagInfo pageInfo) {

          Toast.makeText(SampleActivity.this, "Username: " + responseObject.getUsername(),
                  Toast.LENGTH_LONG).show();
      }

      @Override
      public void onFailure(InstagramException exception) {
          Log.v("SampleActivity", "Exception:" + exception.getMessage());
      }
  };

####Logout user:

InstagramEngine.getInstance(SampleActivity.this).logout();

####Exception Handling: In case of exceptions like insufficient scope or invalid parameters you will get InstagramException in onFailure() of your callback.

InstagramAPIResponseCallback<IGUser> instagramUserResponseCallback = new InstagramAPIResponseCallback<IGUser>() {
  ...
  @Override
  public void onFailure(InstagramException exception) {
      Log.v("Exception", "Exception:" + exception.getMessage());
      }
        
  }

License

Copyright 2016 Sayyam.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitation under the License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.