Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Delphiからニフティクラウドmobile backend 接続
program delphi_nifty_console;
{*******************************************************}
{ }
{ ニフティクラウドmobile backend 接続 }
{ Delphi 10.1Berlin }
{ }
{*******************************************************}
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.DateUtils, System.Hash, System.NetEncoding,
System.Net.HttpClientComponent;
const
def_nifty_signature_str = 'GET'#10
+ 'mb.api.cloud.nifty.com'#10
+ '/2013-09-01/classes/<_class_name_>'#10
+ 'SignatureMethod=HmacSHA256&SignatureVersion=2&'
+ 'X-NCMB-Application-Key=<_X-NCMB-Application-Key_>&'
+ 'X-NCMB-Timestamp=<_X-NCMB-Timestamp_>';
var
nifty_format_string: String;
nifty_application_id,
nifty_secret_code,
nifty_class_name: String;
nifty_iso8601_time: String;
nifty_sha2b: TBytes;
nifty_Signature: String;
http_client: TNetHTTPClient;
begin
try
// 現在時刻(UTC)を ISO8601 の形式で取得
nifty_iso8601_time := DateToISO8601(Now);
// Nifty Cloud mobile backend アプリケーションキーとクライアントキーの設定
nifty_application_id := '08cfcf57fb132dad3b3455e251cc9e4b508bf48b32e46a3af101edfd664df113';
nifty_secret_code := '46134a359c1936e5275115460dbf8158b46c7352eee88cfe892385fc6f993c8e';
nifty_class_name := 'e1';
// 署名用文字列の作成
nifty_format_string := StringReplace(def_nifty_signature_str, '<_X-NCMB-Application-Key_>', nifty_application_id, [rfReplaceAll]);
nifty_format_string := StringReplace(nifty_format_string, '<_X-NCMB-Timestamp_>', nifty_iso8601_time, [rfReplaceAll]);
nifty_format_string := StringReplace(nifty_format_string, '<_class_name_>', nifty_class_name, [rfReplaceAll]);
// 署名用文字列を元にシグネチャを生成
nifty_sha2b := THashSHA2.GetHMACAsBytes(nifty_format_string,nifty_secret_code, THashSHA2.TSHA2Version.SHA256);
nifty_Signature := TNetEncoding.Base64.EncodeBytesToString(nifty_sha2b);
http_client := TNetHTTPClient.Create(nil);
try
// Nifty Cloud mobile backend用のヘッダを作る
http_client.CustomHeaders['X-NCMB-Application-Key'] := nifty_application_id;
http_client.CustomHeaders['X-NCMB-Timestamp'] := nifty_iso8601_time;
http_client.CustomHeaders['X-NCMB-Signature'] := nifty_Signature;
try
// Nifty Cloud mobile backend データストア内のクラス一覧取得
Writeln( http_client.Get('https://mb.api.cloud.nifty.com/2013-09-01/classes/' + nifty_class_name)
.ContentAsString(TEncoding.UTF8) );
except
on e: Exception do
Writeln(e.Message);
end;
finally
http_client.DisposeOf;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment