簡體   English   中英

Instagram訂閱API請求訪問令牌

[英]Instagram Subscription API Asking For Access Token

我一直在使用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&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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM