Instagram订阅API请求访问令牌

时间:2014-03-25 12:28:42

标签: php instagram access-token subscription

我一直在使用Instagram Subscription API订阅Instagram实时更新。我已经成功订阅了Instagram上的多个订阅。但是,当我尝试订阅时,它现在给我以下错误:

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

之前它从未用于为订阅API请求访问令牌。任何人都可以解释Instagram API。

1 个答案:

答案 0 :(得分:3)

太旧但我希望对某些人有帮助。

创建订阅是一个4步骤: -

第一步:将用户引导至我们的授权网址: -

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

第二步:从Instagram接收重定向

作为第1步的回复,Instagram将为您提供http://your-redirect-uri?code=CODE成功,您将在第3步中使用。 注意:CODE不是访问令牌,您将使用CODE获取访问令牌。

第三步:请求access_token: -

POST CURL请求: -

 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

成功 DEMO数据

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

第四步:创建订阅

第四步有一些子步骤。 i)POST curl请求到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)成功后Instagram会提供

   `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:-

在回调页面上,例如:callback.php

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

iii)如果Instagram API将在$_GET['hub_challenge']获得15f7d1a91c1f40f8a748fd134752feb3,则会回复我们的帖子请求以创建

订阅

类似

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

iii)如果成功,您可以直接从您的浏览器列出带有GET请求的订阅。 GET请求: - https://api.instagram.com/v1/subscriptions?client_secret=CLIENT-SECRET&client_id=CLIENT-ID

现在,当经过身份验证的用户发布回调页面时,将获得来自Instagram api的GET请求,其中包含一些包含instagram user_id的JSON数据,您将获得这些数据作为object_id,而media_id是post id。 你可以捕获它并使用下面的代码,是的,你可以使用比我更好的代码,这是伟大的。

 $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);
}