当我们尝试发布身份验证过程第 2 步的请求时,在向https://hamill-murazik-and-johnston6698.myshopify.com/admin/oauth/access_token发送请求时收到 http 400 错误
我们能够通过第 1 步并获得临时代码。但是当我们尝试发布 client_id、client_secret 和代码时,我们得到了 http 400 错误。
代码如下所示
public static void runTest() {
// TODO Auto-generated method stub
try{
URL url = new URL("https://hamill-murazik-and-johnston6698.myshopify.com/admin/oauth/access_token");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
String urlParameters = URLEncoder.encode("client_id", "UTF-8") + "=" + URLEncoder.encode("<CLIENT_ID", "UTF-8");
urlParameters += "&" + URLEncoder.encode("client_secret", "UTF-8") + "=" + URLEncoder.encode("<CLIENT_SECRET", "UTF-8");
urlParameters += "&" + URLEncoder.encode("code", "UTF-8") + "=" + URLEncoder.encode("15f01c0d8b235ffb63234c1c48822432", "UTF-8");
/*connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Content-Length", Integer.toString(102454));
connection.setRequestProperty("Content-Type", "text/plain");*/
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
}