Shopify OAuth第2步问题

Shopify OAuth第2步问题,shopify,Shopify,当我们试图为身份验证过程的第2步发布请求时,向发送请求时会出现http 400错误 我们能够通过步骤1,得到临时代码。但是当我们试图发布客户机id、客户机secret和代码时,我们得到了HTTP400错误 代码如下所示 public static void runTest() { // TODO Auto-generated method stub try{ URL url = new URL("https://hamill-murazik-and-johnston669

当我们试图为身份验证过程的第2步发布请求时,向发送请求时会出现http 400错误

我们能够通过步骤1,得到临时代码。但是当我们试图发布客户机id、客户机secret和代码时,我们得到了HTTP400错误

代码如下所示

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

    }


}
publicstaticvoidruntest(){
//TODO自动生成的方法存根
试一试{
URL=新URL(“https://hamill-murazik-and-johnston6698.myshopify.com/admin/oauth/access_token");           
HttpsURLConnection连接=(HttpsURLConnection)url.openConnection();

字符串urlParameters=URLEncoder.encode(“client_id”,“UTF-8”)+“=”+URLEncoder.encode(“我建议您使用真正的HTTP客户端库,如。很可能您的请求中缺少某些内容或编码不正确

该库还提供了一个更简单的接口,因此您只需为HTTP参数传入键值参数之类的内容,它就可以正常工作

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

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