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
                )

        )

)

Delphi Berlin跨平台條件編譯(Conditional compilation)

Delphi Berlin可以在不同平台裝置上執行,那程式可以依不同平台裝置跑嗎?
答案是可行的~只要透過條件編譯就可以了 範例如下

   {$IFDEF MSWINDOWS}
   LABEL1.Text := Label1.Text + '(電腦版)';
   {$ENDIF}
   {$IFDEF ANDROID}
   LABEL1.Text := Label1.Text + '(安卓版)';

   {$ENDIF}
   {$IFDEF OSX}
   LABEL1.Text := Label1.Text + '(MAC版)';

   {$ENDIF}
   {$IFDEF IOS}
   LABEL1.Text := Label1.Text + '(IOS版)';

   {$ENDIF}

初探Delphi Berlin FireUI APP Preview

Delphi 10.1 Berlin的新功能中,多了一個看起來很棒的FireUI APP Preview功能。主要的功能就是讓開發者在設計畫面時,可以馬上看到畫面長的是什麼樣子。

設定的方式如下:

在10.1的Tools→Options→中有一個 Form Designer,其中有一個 FireUI Live Preview。
fireUIPriew01

fireUIPriew02

fireUIPriew03

client端搜尋FireUI可以找到對應的app,安裝後打開APP會自動搜尋區域網路內的Server。然後連上完成後,設計師在設計的同時就可以觀看在不同裝置上的樣子了

S__11321351 S__11321352 S__11321353

Screenshot_2016-04-24-22-35-12 Screenshot_2016-04-24-22-35-28 Screenshot_2016-04-24-23-04-49

未命名 - 66

 

Embarcadero免費REST除錯工具REST Debugger

REST Debugger這個工具是Embarcadero公司免費釋放給開發REST相關服務的工程師做為測試、除錯用的工具。例如我們想從Sonsterr這個網站取得吉他譜~
一、從Songsterr的API網址
https://www.songsterr.com/a/wa/api

從裡面知道http://www.songsterr.com/a/ra/ 後面帶songs.json即可

二、開啟REST Debugger

將http://www.songsterr.com/a/ra 填入url

RESTDebugger01

進入Parameters頁籤,在Resource填入songs.json,接著按Add按鈕,填入Name pattern value:伍佰,然後按apply確認

RESTDebugger02

最後按Send request按鈕取得內容即可

RESTDebugger03

下載網址: https://www.embarcadero.com/free-tools/rest-debugger

Delphi 10.1 Berlin新亮點 -FireUI Preview、ListView Designer、TAddressBook

感謝廖啟甫大哥通知晚上的 10.1 Berlin課程,雖然沒聽完!不過有聽到了幾個不錯的功能

一、TAddressbook元件,可以存取通訊錄

二、改進Style Designer

三、超酷的可以自訂ListView Items的設計功能

四、新的字型樣式

另外還有很酷的FireUI Preview功能。

看來Delphi一直很努力的追上這幾年的不足!

未命名 - 7 未命名 - 8 未命名 - 21 未命名 - 27 未命名 - 42 未命名 - 44 未命名 - 52 未命名 - 66

MySQL求出二經緯度之間的距離(公尺)

假設自己的經緯度為 LAT,LNG
資料庫欄位_LAT,_LNG,_NAME,
SELECT CEIL((((ACOS(SIN((LAT*PI()/180)) * SIN((_LAT*PI()/180))+COS((LAT*PI()/180))
* COS((_LAT*PI()/180)) * COS(((LNG- _LNG)*PI()/180))))*180/PI())*60*1.1515*1.609344)*1000)
AS DISTANCE,_NAME
FROM TABLE_NAME
ORDER BY DISTANCE ASC
算出距離幾公尺

Delphi 10 Andriod/iOS開發小筆記(常常更新)

System.IOUtils.TPath.GetHomePath

Platform Sample path Path ID
Windows XP C:\Documents and Settings\<username>\Application Data CSIDL_APPDATA
Windows Vista or later C:\Users\<username>\AppData\Roaming FOLDERID_RoamingAppData
Mac OS X /Users/<username> NSUserDirectory
iOS Device /private/var/mobile/Containers/Data/Application/<application ID>
iOS Simulator /Users/<username>/Library/Developer/CoreSimulator/Devices/<Device ID>/data/Containers/Data/Application/<application ID>
Android /data/data/<application ID>/files Context.getFilesDir

For Android, set the Remote Path to assets\internal

For iOS, set the Remote Path to StartUp\Documents

// XE3 : DBJSON
// Seattle: System.JSON
var
  LJsonArr   : TJSONArray;
  LJsonValue : TJSONValue;
  LItem     : TJSONValue;
begin
   memo1.Lines.Clear;
   LJsonArr    := TJSONObject.ParseJSONValue(TEncoding.utf8.GetBytes(memo2.Lines.Text),0) as TJSONArray;
   for LJsonValue in LJsonArr do
   begin
      for LItem in TJSONArray(LJsonValue) do
        memo1.lines.add(Format('%s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
     memo1.Lines.Add('');
   end;
end;

參考: http://blogs.embarcadero.com/davidi/2013/11/23/43005/