java - 服务器返回 HTTP 响应代码 : 401 for url when using shopify API

标签 java shopify

shopify 给出的我的 url 是这种格式
https://apikey:password@hostname/admin/orders.json 因此,当尝试使用 HttpURLConnection 获取订单时,出现 401 未授权错误。这是我的代码

import java.io.*;
import java.net.*;
import java.util.Properties;

/**
 * Created by admin on 22/8/15.
 */
public class Hello {

   // This method should be removed in production
    static void setProxy(){
        Properties systemProperties = System.getProperties();
        systemProperties.setProperty("http.proxyHost","lotus");
        systemProperties.setProperty("http.proxyPort", "8080");
    }
    public static void main(String [] args)
    {
        try
        {

            setProxy();
            URL url = new URL("https://apikey:password@go-frugal.myshopify.com/admin/orders.json");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("user-agent","Mozilla/5.0");
            connection.setRequestProperty("Content-Type","application/json");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            String urlString = "";
            String current;
            while((current = in.readLine()) != null)
            {
                urlString += current;
            }
            System.out.println(urlString);
        }catch(IOException e)
        {
            e.printStackTrace();
        }

    }
}

这里是错误

java.io.IOException: Server returned HTTP response code: 401 for URL: https://apikey:password@go-frugal.myshopify.com/admin/orders.json
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at Hello.main(Hello.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 0<br>

getErrorStream 返回这个

{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

最佳答案

试试这个……你的调用顺序是错误的。希望这会有所帮助。

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.jboss.util.Base64;

public class test9 {

public static void main(String[] args) {
    URL url;
    URLConnection urlConn = null;
    HttpURLConnection htcon = null;
    InputStream is = null;
    StringBuffer sb = new StringBuffer();
    String authStr = "apikey:password";

    String authStringEnc = Base64.encodeBytes(authStr.getBytes());

    //String authStringEnc = new String(Base64Encoder.encode(authString.getBytes()));
    try {
        url = new URL("https:go-frugal.myshopify.com/admin/orders.json");
        urlConn = url.openConnection();
        urlConn.setRequestProperty("Authorization", "Basic " +  authStringEnc);
        urlConn.setRequestMethod("GET");
                urlConn.setRequestProperty("user-agent","Mozilla/5.0");
                urlConn.setRequestProperty("Content-Type","application/json");

        htcon = (HttpURLConnection) urlConn;
        is = htcon.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);

        int numCharsRead;
        char[] charArray = new char[1024];

        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }   

        System.out.println("sb: "+sb);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

关于java - 服务器返回 HTTP 响应代码 : 401 for url when using shopify API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32306509/

相关文章:

database - 连接到 Shopify 或 Wordpress 的 Onix 文件 [帮助]

html - Shopify 页面在移动设备上的显示方式与桌面设备不同

java - A星算法java实现详解

html - 针对 shopify 中的不同模板

javascript - 链接重定向协助

java - 内容类型不支持的问题

api - 请求 Shopify API 以允许创建客户和订单

java - Web 服务还是 Web 应用程序?

java - 从 Sonar 覆盖报告中排除 Lombok 类

java - 在 Swing 中缩小 JPanel