Search code examples
ruby-on-railsoauthoauth-2.0omniauthinstagram

Instagram API - cryptic response; not sure if working and unable to test omniauth with this


I am working on a Rails app that has as the requirement logging in with omniuth to Instagram. This would be my first time using Instagram's OAuth endpoint and it is unclear whether it is working (and not clear to project manager either).

I'm using their cURL implementation with the following (will reset it in future) but getting "No matching code found" error which would seem to be the logical first step.

curl \-F 'client_id=4658fa17d45244c88dd13c73949a57d7' \
    -F 'client_secret=ae6cfe5d13544eada4dece2ec40ac5dc' \
    -F 'grant_type=authorization_code' \
    -F 'redirect_uri= http://seek-style.herokuapp.com/arc/services/instagram' \
    -F 'code=CODE' \https://api.instagram.com/oauth/access_token

with response

{"code": 400, "error_type": "OAuthException", "error_message": "No matching code found."}

Is there something that I am doing obviously wrong? Is there a finished example of how to get this done? I have seen https://github.com/ropiku/omniauth-instagram but can't tell if still working. Spec's pass but the actual call is mocked.

Edit #1

Per comments, I have added a link to repo https://github.com/trestles/seek that is being used to deploy to Heroku. There's basically nothing in the app and the above cURL to the best of my knowledge should be working (and isn't) so I haven't really even tested this. Perhaps I'm misunderstanding even how Instagram's API is working.


Solution

  • So, I have done a little research on Instagram API.

    All the required info is located here: http://instagram.com/developer/authentication/

    First of all, you need to get one-time CODE from Instagram. For your app it is pretty easy.

    Just set href for your 'auth to instagram' link to:

    "https://api.instagram.com/oauth/authorize/?client_id=4658fa17d45244c88dd13c73949a57d7&redirect_uri=http://seek-style.herokuapp.com/arc/services/instagram&response_type=code"
    

    You will receive a redirect from API with CODE as a parameter.

    You can handle it in services_controller#instagram or simply extract from application logs.

    There should be something like

    Processing by ServicesController#instagram as HTML
        Parameters: {"code"=>"c25dcdcb96ed4eb9a508fede0cb94e87", "state"=>"e3d6dc22d6cd3cdebf6fb9e51a728a120a6d901cc382c4bf"}
    

    Then you should request an ACCESS_TOKEN from the API using cURL:

    curl \-F 'client_id=YOUR_CLIENT_ID' \
    -F 'client_secret=YOUR_CLIENT_SECRET' \
    -F 'grant_type=authorization_code' \
    -F 'redirect_uri= http://seek-style.herokuapp.com/arc/services/instagram' \
    -F 'code=CODE_FROM_ABOVE' \https://api.instagram.com/oauth/access_token
    

    Or using RestClient in services_controller#instagram :

    resp = RestClient.post 'https://api.instagram.com/oauth/access_token', {
      client_id: 'YOUR_CLIENT_ID',
      client_secret: 'YOUR_CLIENT_SECRET',
      grant_type: 'authorization_code',
      redirect_uri: 'http://seek-style.herokuapp.com/arc/services/instagram',
      code: params[:code]
    }
    

    The cURL response or resp.body in controller should contain something like:

    {
      "access_token":"1515660384.9f652d3.fcb1e712d41347069ad5c65ccfada994",
      "user":{
        "username":"petro.softserve",
        "bio":"",
        "website":"",
        "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg",
        "full_name":"",
        "id":"1515660384"
      }
    }