facebook寫自動貼文程式時access token權杖時效問題

1465550363703
在寫facebook的自動貼文或程式時,需要有access token,但access token的時效不長。該如何延長時間?
1
進入Graph API測試工具(https://developers.facebook.com/tools/explorer/)取得暫時性的權杖。
2
透過下列網址將APP_ID APP_SECRET ACCESS_TOKEN置換成對應的值

https://graph.facebook.com/oauth/access_token? client_id={APP_ID}& client_secret={APP_SECRET}& fb_exchange_token={SHORTLIVED_ACCESS_TOKEN}& grant_type=fb_exchange_token

3
如果還不行,可以利用Access Token Debugger工具(https://developers.facebook.com/tools/debug/access_token/),將暫時性的權杖輸入後,也可以取得長效(60天)的token。
那如何驗證過期與否重取得??官方給了做法
[php]
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";

// known valid access token stored in a database
$access_token = "YOUR_STORED_ACCESS_TOKEN";

$code = $_REQUEST["code"];

// If we get a code, it means that we have re-authed the user
//and can get a valid access_token.
if (isset($code)) {
$token_url="https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code . "&display=popup";
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params[‘access_token’];
}

// Attempt to query the graph:
$graph_url = "https://graph.facebook.com/me?"
. "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);

//Check for errors
if ($decoded_response->error) {
// check to see if this is an oAuth error:
if ($decoded_response->error->type== "OAuthException") {
// Retrieving a valid access token.
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href=’" . $dialog_url
. "'</script>");
}
else {
echo "other error has happened";
}
}
else {
// success
echo("success" . $decoded_response->name);
echo($access_token);
}

// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes. In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
?>
[/php]

參考:
https://developers.facebook.com/blog/post/2011/05/13/how-to–handle-expired-access-tokens/

LINE@官方帳號機器人PHP試做

1465549488137
試著寫了個PHP for LINE@ BOT API的小程式。
收到的訊息大致像這樣

Array
(
    [result] => Array
        (
            [0] => Array
                (
                    [content] => Array
                        (
                            [toType] => 1
                            [createdTime] => 1465548731679
                            [from] => ubde68bb73a93d53886dc814720423971
                            [location] => 
                            [id] => 4442057145631
                            [to] => Array
                                (
                                    [0] => u0182d0ce673d4b950318618870074d49
                                )

                            [text] => 妳好
                            [contentMetadata] => Array
                                (
                                    [AT_RECV_MODE] => 2
                                    [SKIP_BADGE_COUNT] => true
                                )

                            [deliveredTime] => 0
                            [contentType] => 1
                            [seq] => 
                        )

                    [createdTime] => 1465548731699
                    [eventType] => 138311609000106303
                    [from] => u206d25c2ea6bd87c17655609a1c37cb8
                    [fromChannel] => 1341301815
                    [id] => WB1521-3502883001
                    [to] => Array
                        (
                            [0] => u0182d0ce673d4b950318618870074d49
                        )

                    [toChannel] => 1470435594
                )

        )

)

LINE推出BOT API聊天機器人


LINE 的 BOT API Trial Account可透過 LINE 連結現有系統或服務,設立能傳送或接收 API 驅動訊息的 BOT 帳號,開發出 LINE 使用者與企業間雙向溝通的 API 功能。

https://business.line.me/
https://developers.line.me/bot-api/getting-started-with-bot-api-trial

https://developers.line.me/bot-api/overview

https://github.com/line/line-bot-sdk-php