Instagram 订阅 API 请求访问令牌 [英] Instagram Subscription API Asking For Access Token

查看:38
本文介绍了Instagram 订阅 API 请求访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

meta": {"error_type": "OAuthAccessTokenException",代码":400,"error_message": "提供的 access_token 无效."}

之前它从来没有要求订阅 API 的访问令牌.谁能解释一下 Instagram API.

解决方案

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

创建订阅有 4 个步骤:-

第一步:将您的用户定向到我们的授权 URL:-

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 表示成功,您将在第三步中使用它.注意: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 '代码=代码' https://api.instagram.com/oauth/access_token

成功演示数据

<代码>{"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",用户":{"id": "1574083","username": "snoopdogg","full_name": "史努比狗狗","profile_picture": "..."}}

第四步:创建订阅

第四步有一些子步骤.i) POST curl 请求到 Instagram API

curl -F 'client_id=CLIENT-ID' -F 'client_secret=CLIENT-SECRET' -F '对象=用户' -F '方面=媒体' -F 'verify_token=myVerifyToken' -F 'callback_url=http://YOUR-CALLBACK/URL' https://api.instagram.com/v1/subscriptions/注意:myVerifyToken 应该是任何一个用户的访问令牌,订阅不是为每个用户单独创建的,一个订阅将适用于该应用程序的所有经过身份验证的用户.所以你可以手动提供一个.您不会一次又一次地创建订阅,因此不要调用创建订阅,当您认为需要一个订阅时,只需创建一个,或者通常您会继续订阅一个或删除并重新创建一个.

ii) 成功后 Instagram 将提供

 `https://your-callback.com/url/?hub.mode=subscribe&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.verify_token=myVerifyToken` 其中的回调页面(`http://YOUR-CALLBACK/URL` ) 应该只显示 `hub.challenge`,即:-

在回调页面例如:callback.php

<?php echo $_GET['hub_challenge'];//是的,在点的地方取消划线.?>

iii) 如果 Instagram API 将获得 $_GET['hub_challenge']15f7d1a91c1f40f8a748fd134752feb3 此处它将回复我们的帖子请求以创建订阅>

类似的东西

<代码>{元":{代码":200},数据": [{"id": "1","type": "订阅",对象":用户","方面": "媒体","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,即帖子 ID.您可以使用下面的代码捕获并使用它,是的,您可以使用比我更好的代码,这很棒.

 $content = file_get_contents('php://input');尝试 {如果($content === false){//处理错误//echo '哎呀!出了些问题!';file_put_contents('subscriptions.log', '获取空内容', FILE_APPEND);} 别的 {$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;}} 捕获(异常 $e){//处理异常//echo '哎呀!错误编码的数据接收!';file_put_contents('subscriptions.log', $e->getMessage(), FILE_APPEND);}

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.

解决方案

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

这篇关于Instagram 订阅 API 请求访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆