フォーラム


ゲスト  

ようこそ ゲスト さん。このフォーラムに投稿するには 登録が必要です。

ページ: [1]
トピック: Yahoo! の Web API の使用方法
Mr.XRAY
メンバー
投稿数: 192
Yahoo! の Web API の使用方法
on: 2015/07/08 14:55 Wed

Q ans A に次のような質問がありました.

[YahooAPI(ショップ)の画像アップロードについて]
http://hpcgi3.nifty.com/e-papy/DelphiBBS/wwwlng.cgi?print+201507/15070004.txt

この質問の Web API の記事は以下にあります.

[ショッピング:商品画像アップロードAPI - Yahoo!デベロッパーネットワーク]
http://developer.yahoo.co.jp/webapi/shopping/uploadItemImage.html

Indy の TIdHTTP コンポを使用して,URL でリクエストを送信するものとします.
上の記事の 「サンプルリクエストURL」 を使用することになります.
Yahoo! の Web API の記事によると,パラメータは以下の 2 つになっています.

seller_id
file

上の Yahoo! の記事によると https です.以下を参考にしてください.

[772] [07_HTTPS サーバへのアクセス]
http://mrxray.on.coocan.jp/Delphi/plSamples/772_Indy_HTTPGet.htm#07

URL 文字列で送る場合,「リクエストURL + パラメータ」とします.
パラメータの先頭は ? (クエスチョンマーク) です.パラメータは & (アンパサンド) でつなげます.

上の記事の 「サンプルリクエストURL」の URL では,file がないので,

https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/uploadItemImage?seller_id=teststore&file=hogehoge.jpg

となるでしょう.teststore は実際のストアアカウントの値にします (ストアアカウントはあらかじめ取得しておきます).
HogeHoge の部分は,商品コードか商品コード_(1-5)にするようです.
Yahoo! の記事にも書いてありますが,日本語を含む場合はエンコードが必要ですが,今回は日本語を使用する必要はないでしょう.
パス区切り文字に円記号は使えません.使うとすれは DosPathToUnixPath 関数での変換が必要です.EXE と同じフォルダにファイルを置くといいかも知れません.

POST か GET して TMemoryStream で受け取ります.
(Yahoo! のseller_id のところに赤字で GET と書いてあるので GET かも知れませんが,POST というサンプルリクエストもあるので,どちらかは分かりません.別のページには GET でも… という記述もあるようですが)
受け取ったメモリストリームの中身は,XML コードです.これを解析します.
以下を参考にしてください.

最初の XML テータバインディングが少し面倒ですが,上の Yahoo! のページにひな形があるようです.
多分そのまま使用できると思います.
Yahoo! のページのコードをコピペして,UTF-8 で保存してバインディングして,pas コードを生成します.
バインディングしなくても,XML コードを解析すれば結果は取得できます.

[920] [03_XML データバインディングの手順]
http://mrxray.on.coocan.jp/Delphi/plSamples/920_WebAPI_XML-RPC.htm#03

[921_WebAPI_REST による郵便番号と住所検索]
http://mrxray.on.coocan.jp/Delphi/plSamples/921_WebAPI_REST_Postal.htm

(上の 2 つの記事は,少し難解です.書き直そうと思って,そのままですので,あしからず)

今回は画像のアップということなので,XML の解析は必要ないかも知れませんが.
XML の解析にもいろいろ方法があります.以下は XPATH を使用した簡単な例です.

[314] [04_アプリケーションマニフェストコードの自動編集 - XML の操作例]
http://mrxray.on.coocan.jp/Delphi/plSamples/314_CreateAppManifest.htm#04

【注意】
アプリケーションマニフェストを作成するのに,上記のようなことをする必要はありません.
ネットにあるコードをコピペすれば済みます.

Mr.XRAY
メンバー
投稿数: 192
Re: Yahoo! の Web API の使用方法
on: 2015/07/08 15:06 Wed

>最初の XML テータバインディングが少し面倒ですが,上の Yahoo! のページにひな形があるようです.
>多分そのまま使用できると思います.

実際にコピペして,それを XML データバインディングした結果です.
こんなコードができるという参考に掲載しておきます.
Windows 7 U64(SP1) + Delphi XE(UP1) Pro で作成しました.

 unit ResultSet;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ 前方宣言 }

IXMLResultSetType = interface;
IXMLResultType = interface;
IXMLUrlType = interface;

{ IXMLResultSetType }

IXMLResultSetType = interface(IXMLNode)
['{7D1AAB31-2AB9-49FC-8848-E5CB810C80A7}']
{ プロパティ参照関数 }
function Get_Status: UnicodeString;
function Get_Result: IXMLResultType;
procedure Set_Status(Value: UnicodeString);
{ メソッドとプロパティ }
property Status: UnicodeString read Get_Status write Set_Status;
property Result: IXMLResultType read Get_Result;
end;

{ IXMLResultType }

IXMLResultType = interface(IXMLNode)
['{42C46031-7AB3-46DB-86BC-A8A5196D9ED2}']
{ プロパティ参照関数 }
function Get_Id: UnicodeString;
function Get_Name: UnicodeString;
function Get_Url: IXMLUrlType;
procedure Set_Id(Value: UnicodeString);
procedure Set_Name(Value: UnicodeString);
{ メソッドとプロパティ }
property Id: UnicodeString read Get_Id write Set_Id;
property Name: UnicodeString read Get_Name write Set_Name;
property Url: IXMLUrlType read Get_Url;
end;

{ IXMLUrlType }

IXMLUrlType = interface(IXMLNode)
['{E082970F-0EDA-4665-B3AB-48CEDCA7E9C5}']
{ プロパティ参照関数 }
function Get_ModeA: UnicodeString;
function Get_ModeB: UnicodeString;
function Get_ModeC: UnicodeString;
function Get_ModeD: UnicodeString;
function Get_ModeE: UnicodeString;
function Get_ModeF: UnicodeString;
function Get_ModeG: UnicodeString;
function Get_ModeH: UnicodeString;
function Get_ModeI: UnicodeString;
function Get_ModeJ: UnicodeString;
function Get_ModeK: UnicodeString;
function Get_ModeL: UnicodeString;
procedure Set_ModeA(Value: UnicodeString);
procedure Set_ModeB(Value: UnicodeString);
procedure Set_ModeC(Value: UnicodeString);
procedure Set_ModeD(Value: UnicodeString);
procedure Set_ModeE(Value: UnicodeString);
procedure Set_ModeF(Value: UnicodeString);
procedure Set_ModeG(Value: UnicodeString);
procedure Set_ModeH(Value: UnicodeString);
procedure Set_ModeI(Value: UnicodeString);
procedure Set_ModeJ(Value: UnicodeString);
procedure Set_ModeK(Value: UnicodeString);
procedure Set_ModeL(Value: UnicodeString);
{ メソッドとプロパティ }
property ModeA: UnicodeString read Get_ModeA write Set_ModeA;
property ModeB: UnicodeString read Get_ModeB write Set_ModeB;
property ModeC: UnicodeString read Get_ModeC write Set_ModeC;
property ModeD: UnicodeString read Get_ModeD write Set_ModeD;
property ModeE: UnicodeString read Get_ModeE write Set_ModeE;
property ModeF: UnicodeString read Get_ModeF write Set_ModeF;
property ModeG: UnicodeString read Get_ModeG write Set_ModeG;
property ModeH: UnicodeString read Get_ModeH write Set_ModeH;
property ModeI: UnicodeString read Get_ModeI write Set_ModeI;
property ModeJ: UnicodeString read Get_ModeJ write Set_ModeJ;
property ModeK: UnicodeString read Get_ModeK write Set_ModeK;
property ModeL: UnicodeString read Get_ModeL write Set_ModeL;
end;

{ 前方宣言 }

TXMLResultSetType = class;
TXMLResultType = class;
TXMLUrlType = class;

{ TXMLResultSetType }

TXMLResultSetType = class(TXMLNode, IXMLResultSetType)
protected
{ IXMLResultSetType }
function Get_Status: UnicodeString;
function Get_Result: IXMLResultType;
procedure Set_Status(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLResultType }

TXMLResultType = class(TXMLNode, IXMLResultType)
protected
{ IXMLResultType }
function Get_Id: UnicodeString;
function Get_Name: UnicodeString;
function Get_Url: IXMLUrlType;
procedure Set_Id(Value: UnicodeString);
procedure Set_Name(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;

{ TXMLUrlType }

TXMLUrlType = class(TXMLNode, IXMLUrlType)
protected
{ IXMLUrlType }
function Get_ModeA: UnicodeString;
function Get_ModeB: UnicodeString;
function Get_ModeC: UnicodeString;
function Get_ModeD: UnicodeString;
function Get_ModeE: UnicodeString;
function Get_ModeF: UnicodeString;
function Get_ModeG: UnicodeString;
function Get_ModeH: UnicodeString;
function Get_ModeI: UnicodeString;
function Get_ModeJ: UnicodeString;
function Get_ModeK: UnicodeString;
function Get_ModeL: UnicodeString;
procedure Set_ModeA(Value: UnicodeString);
procedure Set_ModeB(Value: UnicodeString);
procedure Set_ModeC(Value: UnicodeString);
procedure Set_ModeD(Value: UnicodeString);
procedure Set_ModeE(Value: UnicodeString);
procedure Set_ModeF(Value: UnicodeString);
procedure Set_ModeG(Value: UnicodeString);
procedure Set_ModeH(Value: UnicodeString);
procedure Set_ModeI(Value: UnicodeString);
procedure Set_ModeJ(Value: UnicodeString);
procedure Set_ModeK(Value: UnicodeString);
procedure Set_ModeL(Value: UnicodeString);
end;

{ グローバル関数 }

function GetResultSet(Doc: IXMLDocument): IXMLResultSetType;
function LoadResultSet(const FileName: string): IXMLResultSetType;
function NewResultSet: IXMLResultSetType;

const
TargetNamespace = '';

implementation

{ グローバル関数 }

function GetResultSet(Doc: IXMLDocument): IXMLResultSetType;
begin
Result := Doc.GetDocBinding('ResultSet', TXMLResultSetType, TargetNamespace) as IXMLResultSetType;
end;

function LoadResultSet(const FileName: string): IXMLResultSetType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('ResultSet', TXMLResultSetType, TargetNamespace) as IXMLResultSetType;
end;

function NewResultSet: IXMLResultSetType;
begin
Result := NewXMLDocument.GetDocBinding('ResultSet', TXMLResultSetType, TargetNamespace) as IXMLResultSetType;
end;

{ TXMLResultSetType }

procedure TXMLResultSetType.AfterConstruction;
begin
RegisterChildNode('Result', TXMLResultType);
inherited;
end;

function TXMLResultSetType.Get_Status: UnicodeString;
begin
Result := ChildNodes['Status'].Text;
end;

procedure TXMLResultSetType.Set_Status(Value: UnicodeString);
begin
ChildNodes['Status'].NodeValue := Value;
end;

function TXMLResultSetType.Get_Result: IXMLResultType;
begin
Result := ChildNodes['Result'] as IXMLResultType;
end;

{ TXMLResultType }

procedure TXMLResultType.AfterConstruction;
begin
RegisterChildNode('Url', TXMLUrlType);
inherited;
end;

function TXMLResultType.Get_Id: UnicodeString;
begin
Result := ChildNodes['Id'].Text;
end;

procedure TXMLResultType.Set_Id(Value: UnicodeString);
begin
ChildNodes['Id'].NodeValue := Value;
end;

function TXMLResultType.Get_Name: UnicodeString;
begin
Result := ChildNodes['Name'].Text;
end;

procedure TXMLResultType.Set_Name(Value: UnicodeString);
begin
ChildNodes['Name'].NodeValue := Value;
end;

function TXMLResultType.Get_Url: IXMLUrlType;
begin
Result := ChildNodes['Url'] as IXMLUrlType;
end;

{ TXMLUrlType }

function TXMLUrlType.Get_ModeA: UnicodeString;
begin
Result := ChildNodes['ModeA'].Text;
end;

procedure TXMLUrlType.Set_ModeA(Value: UnicodeString);
begin
ChildNodes['ModeA'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeB: UnicodeString;
begin
Result := ChildNodes['ModeB'].Text;
end;

procedure TXMLUrlType.Set_ModeB(Value: UnicodeString);
begin
ChildNodes['ModeB'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeC: UnicodeString;
begin
Result := ChildNodes['ModeC'].Text;
end;

procedure TXMLUrlType.Set_ModeC(Value: UnicodeString);
begin
ChildNodes['ModeC'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeD: UnicodeString;
begin
Result := ChildNodes['ModeD'].Text;
end;

procedure TXMLUrlType.Set_ModeD(Value: UnicodeString);
begin
ChildNodes['ModeD'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeE: UnicodeString;
begin
Result := ChildNodes['ModeE'].Text;
end;

procedure TXMLUrlType.Set_ModeE(Value: UnicodeString);
begin
ChildNodes['ModeE'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeF: UnicodeString;
begin
Result := ChildNodes['ModeF'].Text;
end;

procedure TXMLUrlType.Set_ModeF(Value: UnicodeString);
begin
ChildNodes['ModeF'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeG: UnicodeString;
begin
Result := ChildNodes['ModeG'].Text;
end;

procedure TXMLUrlType.Set_ModeG(Value: UnicodeString);
begin
ChildNodes['ModeG'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeH: UnicodeString;
begin
Result := ChildNodes['ModeH'].Text;
end;

procedure TXMLUrlType.Set_ModeH(Value: UnicodeString);
begin
ChildNodes['ModeH'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeI: UnicodeString;
begin
Result := ChildNodes['ModeI'].Text;
end;

procedure TXMLUrlType.Set_ModeI(Value: UnicodeString);
begin
ChildNodes['ModeI'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeJ: UnicodeString;
begin
Result := ChildNodes['ModeJ'].Text;
end;

procedure TXMLUrlType.Set_ModeJ(Value: UnicodeString);
begin
ChildNodes['ModeJ'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeK: UnicodeString;
begin
Result := ChildNodes['ModeK'].Text;
end;

procedure TXMLUrlType.Set_ModeK(Value: UnicodeString);
begin
ChildNodes['ModeK'].NodeValue := Value;
end;

function TXMLUrlType.Get_ModeL: UnicodeString;
begin
Result := ChildNodes['ModeL'].Text;
end;

procedure TXMLUrlType.Set_ModeL(Value: UnicodeString);
begin
ChildNodes['ModeL'].NodeValue := Value;
end;

end.
DEKO
管理者
投稿数: 2690
Re: Yahoo! の Web API の使用方法
on: 2015/07/09 23:16 Thu

おー!

Mr.XRAY
メンバー
投稿数: 192
Re: Yahoo! の Web API の使用方法
on: 2015/07/28 10:47 Tue

引用 Mr.XRAY on 2015/07/08 14:55 Wed
POST か GET して TMemoryStream で受け取ります.
(Yahoo! のseller_id のところに赤字で GET と書いてあるので GET かも知れませんが,POST というサンプルリクエストもあるので,どちらかは分かりません.別のページには GET でも… という記述もあるようですが)

 
人心を惑わすようなことを書いてしまいましたが,REST の場合は GET コマンドです.
Indy の TIdHTTP を使用する場合は,GET メソッドです.

Delphi XE7 には REST 用のコンポーネントがあります.以下を参考にしてください.
この記事では,JSON 形式で結果を取得しますが (マルチデバイス対応),私のサイトの記事のサンプルでは,XML 形式で取得します.
JSON で取得するか XML で取得するかは,提供している Web API によります.該当する Web API がその形式を提供していないと取得できません.

[Delphi XE7 の REST コンポーネントを使って、天気予報を取得する]
http://www.gesource.jp/weblog/?p=6859

ページ: [1]
WP Forum Server by ForumPress | LucidCrew
バージョン: 1.7.5 ; ページロード: 0.044 sec.