Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

php - Instagram Subscription API Asking For Access Token

I have been using Instagram Subscription API to subscribe to Instagram real time updates. I have successfully subscribed to multiple subscriptions on Instagram. But now it is giving me the following error when I try to subscribe:

meta": {
    "error_type": "OAuthAccessTokenException",
    "code": 400,
    "error_message": "The access_token provided is invalid."
}

Earlier it never used to ask for access token for subscription API. Can anyone please explain Instagram API.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Too old but I hope will be helpful to some people.

Creating a subscription is 4 step process:-

Step One: Direct your user to our authorization URL:-

GET request :- https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

Step Two: Receive the redirect from Instagram

As a response of step 1, Instagram will provide you a http://your-redirect-uri?code=CODE on success, which you will use in step three. Note: CODE is not access token, you will use CODE to get access token.

Step Three: Request the access_token:-

POST CURL Request :-

 curl -F 'client_id=CLIENT_ID' 
    -F 'client_secret=CLIENT_SECRET' 
    -F 'grant_type=authorization_code' 
    -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' 
    -F 'code=CODE' 
    https://api.instagram.com/oauth/access_token

on success DEMO Data

{
    "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
    "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "..."
    }
}

Step Four: Creating a Subscription

Step Four has some sub steps. i) POST curl request to Instagram API

curl -F 'client_id=CLIENT-ID' 
     -F 'client_secret=CLIENT-SECRET' 
     -F 'object=user' 
     -F 'aspect=media' 
     -F 'verify_token=myVerifyToken' 
     -F 'callback_url=http://YOUR-CALLBACK/URL' 
     https://api.instagram.com/v1/subscriptions/

Note: myVerifyToken should be a access token of any one user, subscription is not created separately for every user, one subscription will be working for all the authenticated user of this app. so you may manually provide one. You do not create subscription again and again, so do not make calls to create subscription, when ever you think you need one subscription then only create one or usually you will continue with one or delete and recreate one.

ii) On success Instagram will provide

   `https://your-callback.com/url/?hub.mode=subscribe&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.verify_token=myVerifyToken` of which the callback page ( `http://YOUR-CALLBACK/URL` ) should only display `hub.challenge` that is:-

on callback page eg: callback.php

<?php echo $_GET['hub_challenge']; //yes undescore in palce of dot.  ?>

iii) If the Instagram API will get $_GET['hub_challenge'] that is 15f7d1a91c1f40f8a748fd134752feb3 here it will reply for our post request to create a subscription with

something like

{
    "meta": {
        "code": 200
    },
    "data": [
        {
            "id": "1",
            "type": "subscribe",
            "object": "user",
            "aspect": "media",
            "callback_url": "https://your-callback.com/url/"
        }
    ]
}

iii) If success you can list out the subscription with a GET request may be directly from your browser. GET Request:- https://api.instagram.com/v1/subscriptions?client_secret=CLIENT-SECRET&client_id=CLIENT-ID

now when ever the authenticated users will post the callback page will get a GET request from Instagram api with some JSON data containing instagram user_id that you will get as object_id and media_id that is the post id. You can catch that and use that with the below code, yes you can use better code than me , that is GREAT.

 $content = file_get_contents('php://input');
try {
    if ($content === false) {
        // Handle the error
        //echo 'Whoops! Something went wrong!';
        file_put_contents('subscriptions.log', 'getting empty content', FILE_APPEND);
    } else {
        $content_object = json_decode($content)[0];
        $error = json_last_error();
        file_put_contents('subscriptions.log', $error, FILE_APPEND);
        $ig_id = $content_object->object_id;
        $media_id = $content_object->data->media_id;
    }
} catch (Exception $e) {
    // Handle exception
    //echo 'Whoops! Wrongly encoded data receiving!';
    file_put_contents('subscriptions.log', $e->getMessage(), FILE_APPEND);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...