gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:29 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com