问题
I'm having a heck of a time trying to figure out this Yahoo Store API stuff. I've scoured the internet for examples but have come up with next to nothing. I've created my request:
String data = "";
data += "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
data += "<ystorewsRequest>";
data += "<StoreID>" + storeID + "</StoreID>";
data += "<SecurityHeader>";
data += "<PartnerStoreContractToken>" + token + "</PartnerStoreContractToken>";
data += "</SecurityHeader>";
data += "<Version> 1.0 </Version>";
data += "<Verb> get </Verb>";
data += "<ResourceList>";
data += "<OrderListQuery>";
data += "<Filter>";
data += "<Include> all </Include>";
data += "</Filter>";
data += "<QueryParams>";
data += "<OrderID> 5441 </OrderID>";
data += "</QueryParams>";
data += "</OrderListQuery>";
data += "</ResourceList>";
data += "</ystorewsRequest>";
and have attempted to send the data to the URL listed in the API doc: https://MyStoreID.order.store.yahooapis.com/V1/order (stored in String address)
url = new URL(address);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setRequestMethod("POST");
String urlParameters = "query=" + data;
DataOutputStream wr = new DataOutputStream (
conn.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
line = rd.readLine();
rd.close();
I'm getting this error as a result;
java.io.IOException: Server returned HTTP response code: 400 for URL: https://MyStoreID.order.store.yahooapis.com/V1/order
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
I'm pretty much completely lost with this as yahoo provides some poor documentation and no examples that I could find. Has anyone ever tried to connect to a Yahoo Store using Yahoo API calls from Java? Any help at this point is appreciated. Thanks.
回答1:
I was able to figure this out. So I will post the solution for everyone else. This is a PHP script that will request all information about order number 5863. I can call the PHP script from a java program and parse the result as needed from there.
<?php
//build xml request
$data = "<?xml version='1.0' encoding='utf-8'?>";
$data .= "<ystorewsRequest>";
$data .= "<StoreID>your store id</StoreID>"; //insert your store id
$data .= "<SecurityHeader>";
$data .= "<PartnerStoreContractToken>your token</PartnerStoreContractToken>"; //insert your token`
$data .= "</SecurityHeader>";
$data .= "<Version>1.0</Version>";
$data .= "<Verb>get</Verb>";
$data .= "<ResourceList>";
$data .= "<OrderListQuery>";
$data .= "<Filter>";
$data .= "<Include>all</Include>";
$data .= "</Filter>";
$data .= "<QueryParams>";
$data .= "<OrderID>5863</OrderID>";
$data .= "</QueryParams>";
$data .= "</OrderListQuery>";
$data .= "</ResourceList>";
$data .= "</ystorewsRequest>";
//send request to yahoo order api
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, "https://your_store_id.order.store.yahooapis.com/V1/order"); //insert your store id
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content=curl_exec($ch);
//print raw xml data returned from yahoo
echo htmlentities( $content);
?>
来源:https://stackoverflow.com/questions/15753012/yahoo-store-order-api-access