简体   繁体   中英

curl to request in java

I have to make a request to an api. The curl that works is:

curl -u apiKey:pass -H "Accept: application/json" https://subdomain.chargify.com/portal/customers/id/management_link.json

and the java code that i have so far is:

String userpass = apiKey + ":" + pass;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));

URL url = new URL(stringUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("Accept", "application/json");
uc.setRequestProperty("Authorization", basicAuth);

InputStream content = uc.getInputStream();
int status = uc.getResponseCode();
BufferedReader in = new BufferedReader (new InputStreamReader(content));

String line;
while ((line = in.readLine()) != null) {
        System.out.println(line);
}

Every time i get a 401 response code. What am I doing wrong?

Your request requires User Aunthentication. As the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. Check how you are generating and validating key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM