Shopify OAuth第2步问题

时间:2012-10-25 20:27:02

标签: shopify

当我们尝试发布身份验证过程第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();

    }


}

1 个答案:

答案 0 :(得分:0)

我建议您使用真正的HTTP客户端库,例如Apache HttpClient library。很可能您在请求中遗漏了某些内容或编码错误。

库也会提供一个更简单的界面来处理,所以你可以传递你的HTTP参数的键值参数之类的内容,它只会起作用。

此外,您似乎正在将您的授权请求附加到您的网址参数中。您需要为该请求发出POST请求。在你当前的实现中,你必须在Body中发送它,这在普通的Java中可能很尴尬。

我强烈建议您使用HttpClient库,并告诉我们是否有帮助您。

相关问题