首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >想要使oauth2的perl代码在Java中工作:oauth2 -H的授权: Bearer:<TOKEN> Bearer:<TOKEN>

想要使oauth2的perl代码在Java中工作:oauth2 -H的授权: Bearer:<TOKEN> Bearer:<TOKEN>
EN

Stack Overflow用户
提问于 2016-04-06 23:15:47
回答 3查看 441关注 0票数 0

我得到了一个错误,我是未经授权的。响应代码401

我使用的标记是perl中的工作。

到目前为止,我一直在尝试这样做:

代码语言:javascript
复制
    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();           
        }
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-12 01:31:39

代码语言:javascript
复制
    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();           
    }
}
票数 1
EN

Stack Overflow用户

发布于 2016-04-06 23:44:23

您已经将其注释掉了,但是我处理过的每一个Oauth服务器都是"Bearer“--只有一个空间,没有冒号。

票数 0
EN

Stack Overflow用户

发布于 2016-04-07 02:49:12

在调用之后设置参数!

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

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36463975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档