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

Arduino自動灌溉系統

作用: 一般的灑水系統是時間自動到時灑水,但與實際狀況不符。可以利用土壤溼度計偵測土壤的溼度再予以灌溉。
材料: Arduino *1 、麵包版*1、16*2 LCD*1 、土壤溼度計*1、溫溼度計*1、繼電器*1、太陽能電子板*1、抽水馬達*1
程式:
#include
#include
#define dht_dpin A1 // 溫濕度資料腳位
LiquidCrystal_I2C lcd(0x27, 16, 2); // 顯示器
byte bGlobalErr;
byte dht_dat[5];
int relay = 10; //繼電器腳位為10

void setup() {
InitDHT();
delay(300);
lcd.begin();
lcd.backlight();
lcd.print(“Lin Shou Shan”);
pinMode(relay,OUTPUT); //定義繼電器腳位10為輸出
}

void loop() {
ReadDHT();
lcd.clear();
switch (bGlobalErr){
case 0:
String TT,HH;
HH = String(dht_dat[0])+’.’+String(dht_dat[1]);
TT = String(dht_dat[2])+’.’+String(dht_dat[3]);
lcd.setCursor(0, 0);
lcd.print(“H:”+HH+” T:”+TT); // 第一行顯示空氣溼度/溫度
}
lcd.setCursor(0, 1);
int aa;
aa = analogRead(0);
lcd.print(“Value:”+String(analogRead(0))); // 第二行顯示土壤溼度值
if (aa < 300){ // 濕度小於範圍就啟動澆水 autowatering(); } delay(10000); // 10秒執行一次 } void autowatering(){ digitalWrite(relay,HIGH); //開啟馬達 delay(5000); // 抽水三秒 digitalWrite(relay,LOW); //關閉繼電器 delay(1000); } void InitDHT(){ // 初始化溫溼度計 pinMode(dht_dpin,OUTPUT); digitalWrite(dht_dpin,HIGH); } void ReadDHT(){ bGlobalErr=0; byte dht_in; byte i; digitalWrite(dht_dpin,LOW); delay(20); digitalWrite(dht_dpin,HIGH); delayMicroseconds(40); pinMode(dht_dpin,INPUT); dht_in=digitalRead(dht_dpin); if(dht_in){ bGlobalErr=1; return; } delayMicroseconds(80); dht_in=digitalRead(dht_dpin); if(!dht_in){ bGlobalErr=2; return; } delayMicroseconds(80); for (i=0; i<5; i++) dht_dat[i] = read_dht_dat(); pinMode(dht_dpin,OUTPUT); digitalWrite(dht_dpin,HIGH); byte dht_check_sum = dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3]; if(dht_dat[4]!= dht_check_sum) {bGlobalErr=3;} }; byte read_dht_dat(){ byte i = 0; byte result=0; for(i=0; i< 8; i++){ while(digitalRead(dht_dpin)==LOW); delayMicroseconds(30); if (digitalRead(dht_dpin)==HIGH) result |=(1<<(7-i)); while (digitalRead(dht_dpin)==HIGH); } return result; }