Shopify OAuth step 2 issue

When we try to post a request for step 2 of the authentication process, we get an http 400 error when sending request to https://hamill-murazik-and-johnston6698.myshopify.com/admin/oauth/access_token

We are able to get past step 1 and we get the temp code. But when we try to post the client_id, client_secret and code, we get back the http 400 error.

Code is shown below

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();

    }


}

I'd suggest you use a real HTTP Client library such as the Apache HttpClient library. There is a good chance you are missing something in your request or encoding something incorrectly.

The library wil provide a simpler interface to work with as well, so you can just pass in things like key-value parameters for your HTTP params and it'll just work.

Also, it appears that you are appending your authorize request into your URL parameters. You need to make a POST request for that request. In your current implementation you'd have to send that in the Body, which can be awkward in plain Java.

I highly suggest using the HttpClient library, and letting us know if that helped you out.

链接地址: http://www.djcxy.com/p/68434.html

上一篇: 谷歌oauth访问令牌411响应

下一篇: Shopify OAuth步骤2问题