2014-03-25 134 views
1

我一直在使用Instagram訂閱API訂閱Instagram實時更新。我已成功訂閱Instagram上的多個訂閱。但是,現在這是給我下面的錯誤,當我嘗試訂閱:Instagram訂閱API詢問訪問令牌

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

此前它從未用來詢問訂閱API訪問令牌。任何人都可以請解釋Instagram的API。

+0

爲了能夠使用Instagram API,你必須註冊你的應用程序。你做過了嗎? –

+0

是的,我提到我已經在Instagram上有多個訂閱實時更新。它現在恰好發生它給訪問令牌空錯誤。其餘的參數全部由我提供。而且我能夠在沒有訪問令牌的情況下訂閱。 – Maverik

+0

你從哪裏看到這些數據?在從訂閱中檢索有效內容時在GET調用中?或者當試圖*註冊*新的訂閱? – brandonscript

回答

3

太舊了,但我希望能對一些人有所幫助。

創建預訂爲4個步驟: -

步驟一:將您的用戶給我們的授權網址: -

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

第二步:接收重定向, from Instagram

作爲步驟1的迴應,Instagram會爲您提供http://your-redirect-uri?code=CODE成功,您將在第三步中使用。 注意:CODE不是訪問令牌,您將使用CODE獲取訪問令牌。

第三步:請求的access_token: -

POST捲曲請求: -

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 
成功

演示數據

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

第四步:創建訂閱

第四步有一些子步驟。 我)POST捲曲請求,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. 

二)關於成功的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/" 
     } 
    ] 
} 

三)如果成功,你可以列出一個GET請求,可以從瀏覽器直接訂閱。 GET請求: - https://api.instagram.com/v1/subscriptions?client_secret=CLIENT-SECRET&client_id=CLIENT-ID

現在,當曾經的身份驗證的用戶將發佈回調頁面將獲得來自Instagram的API,包含的Instagram USER_ID,你會得到作爲OBJECT_ID和media_id是的帖子ID一些JSON數據的GET請求。 你可以捕捉並使用下面的代碼,是的,你可以使用比我更好的代碼,這是偉大的。

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