質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

1回答

7642閲覧

Yahoo ショッピングAPI について

teratail_user

総合スコア14

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2016/05/13 05:28

編集2016/05/13 05:32

##Yahoo ショッピング API について質問です。

ヤフーの店舗向け、受注連携プログラムを開発中です。
ヤフーのショッピングAPIを使って注文詳細やステータス変更をしたかったのですが、
できなくて困っています。お分かりになるかた、アドバイスお願い致します。

###【やりたいこと】
① 注文番号から、注文の詳細を取得する
② 任意の注文番号の注文のステータスを任意に変更する

###【やった事】
1)本番環境アプリケーションIDおよびシークレットIDを申請・取得
2)1)と、Yahooが提供しているYahooID連携用のSDK (YConnectExplicitクラス)を利用してリフレッシュトークンを行い、アクセストークンを取得

###【詰まっている事】
2)で取得したアクセストークンを利用して、 やりたいことの① の取得を試みたが、”401エラー” となって取得できなかった。

→ 詳細

getYahooOrderEntity というメソッドに ショップID(sellerId)と注文番号(orderId)を渡すと、
このメソッド内で指定しているフィールド(お届け先や商品情報) を Entityにいれて取得する、という処理です。
(いまはテストで return が void となっています)

認証エラーとなっていますが、”有効なアクセストークン” は間違いなく有効です。
この”有効なアクセストークン”を利用してユーザー情報(店情報)を取得することに成功しています。
(プログラムを実行するたびリフレッシュトークンを行ってから下記のメソッドを実行しています。)

// connection.getResponseMessage() で 「Authorization Required」
↓ のところで問題です

Java

1public Entity getYahooOrderEntity(String sellerId, String orderId) { 2 String requestString = ""; 3 try { 4 URLEncoder.encode(orderId, "UTF-8"); 5 6 String field = "OrderId,DeviceType,OrderTime,OrderStatus,StoreStatus,SendConfirmTime,SendPayTime," + 7 "BuyerComments,SellerComments,TotalCouponDiscount,BillFirstName,BillLastName,BillZipCode,BillPrefecture," + 8 "BillCity,BillAddress1,BillAddress2,BillPhoneNumber,BillMailAddress,ShipMethodName,ShipRequestDate,ShipRequestTime," + 9 "ShipNotes,ShipFirstName,ShipLastName,ShipZipCode,ShipPrefecture,ShipCity,ShipAddress1,ShipAddress2,ShipPhoneNumber," + 10 "PayCharge,ShipCharge,Discount,Adjustments,SettleAmount,TotalPrice,SettlePayAmount,ItemId,Title,Jan,ProductId," + 11 "UnitPrice,Quantity,ReleaseDate,IsShippingFree"; 12 13 // リクエスト内容 14 requestString += 15 "OrderId=" + URLEncoder.encode(orderId, "UTF-8") + "&" + 16 "Field=" + URLEncoder.encode(field, "UTF-8") + "&" + 17 "SellerId=" + URLEncoder.encode(sellerId, "UTF-8"); 18 } catch (Exception e) { 19 20 } 21 executePost("https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/orderInfo", requestString); 22 } 23 24private void executePost(String requestRrl, String requestString) { 25 System.out.println("===== HTTP POST Start ====="); 26 try { 27 URL url = new URL(requestRrl); 28 HttpURLConnection connection = null; 29 try { 30 connection = (HttpURLConnection) url.openConnection(); 31 connection.setDoOutput(true); 32 connection.setRequestMethod("POST"); 33 OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); 34 BufferedWriter writer = new BufferedWriter(osw); 35 int contentLength = requestString.length(); 36 writer.write("POST /ShoppingWebService/V1/orderInfo HTTP/1.1"); 37 writer.write("Host: circus.shopping.yahooapis.jp"); 38 writer.write("Authorization: Bearer " + 有効なアクセストークン); 39 writer.write("Content-Type: application/x-www-form-urlencoded"); 40 writer.write("Content-Length: " + contentLength); 41 writer.write(requestString); 42 writer.flush(); 43 44 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 45 // レスポンスを使って処理 46 } 47 // connection.getResponseCode() で 401 が返ってきて、 48 // connection.getResponseMessage() で 「Authorization Required」 49 System.out.println(connection.getResponseMessage()); 50 } finally { 51 if (connection != null) { 52 connection.disconnect(); 53 } 54 } 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 System.out.println("===== HTTP POST End ====="); 59 }

###【聞きたいこと】
① Authorization Required を直訳すると「認証が必要」ということですが、executePost() 内の writer.write("Authorization: Bearer " + 有効なアクセストークン); で認証ということではないのでしょうか。
別に認証処理があるのでしょうか…?

② この次のステップなのですが、ショッピングAPIにリクエストを投げる方法として、&でつなぐ形でもいいのでしょうか。サンプルみるとXML形式みたいになってますが、XML形式みたいなのにしたほうがいいのでしょうか。
YahooのほかのAPIのサンプルでは&でつなげていたのですが、ショッピングAPIはXMLみたいなのになってました。

③ 追加ですみません、
ヤフーショッピングAPIを利用するにあたり「API本番環境利用申請」をまだ行っていません。
こちらの申請も必要になるのでしょうか。よろしくお願い致します。

参考にしたページ

注文詳細API
http://developer.yahoo.co.jp/webapi/shopping/orderInfo.html

WebAPIの使い方(POSTリクエスト)
http://developer.yahoo.co.jp/appendix/request/rest/post.html

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決しました。

Authorization はヘッダーでするんですね。
あと ③ もやらないでも本物の注文情報が取得できました^^;

参考にしたサイト↓

再入門!HTTP/2.0が主流になる前にHTTP/1.1の基本を知ろう
http://www.casleyconsulting.co.jp/blog-engineer/%E6%9C%AA%E5%88%86%E9%A1%9E/%E5%86%8D%E5%85%A5%E9%96%80%EF%BC%81%EF%BC%81http2-0%E3%81%8C%E4%B8%BB%E6%B5%81%E3%81%AB%E3%81%AA%E3%82%8B%E5%89%8D%E3%81%ABhttp1-1%E3%81%AE%E5%9F%BA%E6%9C%AC%E3%82%92%E7%9F%A5%E3%82%8D%E3%81%86/

ソース修正後

Java

1 public void getYahooOrderEntity(String sellerId, String orderId) { 2 String requestString = ""; 3 try { 4 URLEncoder.encode(orderId, "UTF-8"); 5 String field = "OrderId,DeviceType,OrderTime,OrderStatus,StoreStatus,SendConfirmTime,SendPayTime," + 6 "BuyerComments,SellerComments,TotalCouponDiscount,BillFirstName,BillLastName,BillZipCode,BillPrefecture," + 7 "BillCity,BillAddress1,BillAddress2,BillPhoneNumber,BillMailAddress,ShipMethodName,ShipRequestDate,ShipRequestTime," + 8 "ShipNotes,ShipFirstName,ShipLastName,ShipZipCode,ShipPrefecture,ShipCity,ShipAddress1,ShipAddress2,ShipPhoneNumber," + 9 "PayCharge,ShipCharge,Discount,Adjustments,SettleAmount,TotalPrice,SettlePayAmount,ItemId,Title,Jan,ProductId," + 10 "UnitPrice,Quantity,ReleaseDate,IsShippingFree"; 11// requestString += 12// "OrderId=" + URLEncoder.encode(orderId, "UTF-8") + "&" + 13// "Field=" + URLEncoder.encode(field, "UTF-8") + "&" + 14// "SellerId=" + URLEncoder.encode(sellerId, "UTF-8"); 15 requestString = 16 "<Req>" + 17 "<Target>" + 18 "<OrderId>" + orderId + "</OrderId>" + 19 "<Field>" + field + "</Field>" + 20 "</Target>" + 21 "<SellerId>" + sellerId + "</SellerId>" + 22 "</Req>"; 23 } catch (Exception e) { 24 25 } 26 executePost("https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/orderInfo", requestString); 27 } 28 29 private void executePost(String requestRrl, String requestString) { 30 System.out.println("===== HTTP POST Start ====="); 31 try { 32 URL url = new URL(requestRrl); 33 HttpURLConnection connection = null; 34 try { 35 connection = (HttpURLConnection) url.openConnection(); 36 connection.setDoInput(true); 37 connection.setDoOutput(true); 38 connection.setRequestMethod("POST"); 39 40 connection.setRequestProperty("Host", "circus.shopping.yahooapis.jp"); 41 connection.setRequestProperty("Authorization", "Bearer " + 有効なアクセストークン); 42 43 OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); 44 BufferedWriter writer = new BufferedWriter(osw); 45 //int contentLength = requestString.length(); 46 //writer.write("POST /ShoppingWebService/V1/orderInfo HTTP/1.1"); 47 //writer.write("Host: circus.shopping.yahooapis.jp"); 48 //writer.write("Authorization: Bearer " + tmYahooAuth.getAccessToken()); 49 //writer.write("Content-Type: application/x-www-form-urlencoded"); 50 //writer.write("Content-Length: " + contentLength); 51 //writer.write("\n"); 52 writer.write(requestString); 53 writer.flush(); 54 55 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 56 try { 57 InputStreamReader isr = new InputStreamReader(connection.getInputStream(), "UTF-8"); 58 BufferedReader reader = new BufferedReader(isr); 59 String line; 60 while ((line = reader.readLine()) != null) { 61 System.out.println(line); 62 } 63 } catch (Exception e) {} 64 } 65 System.out.println(connection.getResponseMessage()); 66 } finally { 67 if (connection != null) { 68 connection.disconnect(); 69 } 70 } 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 75 System.out.println("===== HTTP POST End ====="); 76 }

投稿2016/05/13 08:00

teratail_user

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問