微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Instagram订阅API请求访问令牌

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

Meta": {
    "error_type": "OAuthAccesstokenException",
    "code": 400,
    "error_message": "The access_token provided is invalid."
}

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

解决方法:

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

创建订阅是4个步骤: –

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

GET请求: – https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID\u0026amp;redirect_uri=REDIRECT-URI\u0026amp;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\u0026amp;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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐