简体   繁体   中英

Want to make perl code for oauth2 work in Java “curl -H 'Authorization: Bearer:<TOKEN> https://canvas.instructure.com/api/v1”;

I am getting an error that I am unauthorized.. RESPONSE CODE 401
The token I am using works in perl..

This is what I have tried till now:

    public static void main(String[] args) throws Exception {
        try {
        String auth = returnAuth(); //getting token from a file.
        //System.out.println(auth);
        String url1= "https://canvas.instructure.com/api/v1";
        URL url = new URL(url1+"/courses");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");     

        //connection.setRequestProperty("Authorization", "Bearer " + auth);

        connection.setRequestProperty("Authorization", "Bearer "+auth);
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response code:" + connection.getResponseCode());
        System.out.println("Response message:" + connection.getResponseMessage());

            // Read the response:
            BufferedReader reader = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            System.out.println(response.toString());
        }
        catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();           
        }
    }
    public static void main(String[] args) throws Exception { 
    try {
    String auth = returnAuth(); //getting token from a file.
    String url1= "https://canvas.instructure.com/api/v1/courses";
    URL url = new URL(url1);
    HttpsURLConnection connection =(HttpsURLConnection) url.openConnection();
    connection.setRequestMethod("GET");     

    connection.setRequestProperty("Authorization", "Bearer "+auth);
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response code:" + connection.getResponseCode());
    System.out.println("Response message:" + connection.getResponseMessage());

        // Read the response:
        BufferedReader reader = new BufferedReader(new InputStreamReader(
        connection.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }
    catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();           
    }
}

您已将其注释掉,但是我处理过的每个Oauth服务器都将是“ Bearer <token>”-一个空格,没有冒号。

You set the parameters AFTER you called!

Change your code to first set the authentication, then open the connection.

String url1= "https://canvas.instructure.com/api/v1";
URL url = new URL(url1+"/courses");
connection.setRequestProperty("Authorization", "Bearer "+auth);
connection.setRequestMethod("GET");     

//now you can open the connection...

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

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