0

我收到一个我未经授权的错误.. RESPONSE CODE 401
我使用的令牌在 perl..

这是我到目前为止所尝试的:

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

3 回答 3

1
    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();           
    }
}
于 2016-04-12T01:31:39.660 回答
0

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

于 2016-04-06T23:44:23.630 回答
0

调用后设置参数!

更改您的代码以首先设置身份验证,然后打开连接。

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();
于 2016-04-07T02:49:12.920 回答