swo2 SOA OAuth 使用,

news/2024/7/23 23:54:55


 

分三步: 

1:申请 账号,密码 

2:得到token

3:使用token,调用api


 

2 得到token

 

public String getToken() {

    String token = "";

    String authorization = new BASE64Encoder().encode((consumerKey + ":" + consumerSecret).getBytes());

    authorization = authorization.trim();

    OkHttpClient client = new OkHttpClient();

    RequestBody formBody = new FormEncodingBuilder().add("grant_type", "client_credentials").build();

    Request request = new Request.Builder().url(tokenUrl).post(formBody)

            .addHeader("authorization", "Basic " + authorization)

            .addHeader("content-type", "application/x-www-form-urlencoded").addHeader("cache-control", "no-cache")

            .build();

    try {

        Response response = client.newCall(request).execute();

        if (response.isSuccessful()) {

            String result = response.body().string();

            JSONObject ret = JSONObject.parseObject(result);

            token = ret.getString("access_token");

            return token;

        }

    } catch (IOException e) {

        e.printStackTrace();

    }

 

    return token;

}

 

3. 使用token 调用api

 

public String testOSP(String param) {

    String result = "";

    String token = wso2Uitil.getToken();  

    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), param);

    Request request = new Request.Builder().url(ospurl).post(body)

            .addHeader("authorization", "Bearer " + token)

            .addHeader("cache-control", "no-cache").build();

    try {

        Response response = client.newCall(request).execute();

        result = response.body().string();

    } catch (IOException e) {

        e.printStackTrace();

    }

 

    return result;

}

 


http://www.niftyadmin.cn/n/958209.html

相关文章

装cnpm的命令

npm install -g cnpm --registryhttp://registry.npm.taobao.org

Mybatis 获得自动生成主键值

介绍 对于某些特殊需求:得到刚刚插入数据的主键值,以便对刚刚生成的数据做处理 那么,如何得到刚插入的主键值呢有两种大的方向。 第一是在数据库获得通过自带方法。在数据插入之后输入“select indentity”通常需要结合存储过程&#xff0…

java SASL_SSL 帐号密码 方式访问 kafka

java SASL_SSL 帐号密码 方式访问 kafka Producer Java Sample java生产者: Properties props new Properties(); props.put("bootstrap.servers", "*******:9092,*******:9092"); props.put("acks", "all");// props.put("…

字符串多维数组

字符串&多维数组 字符串 (1)串的逻辑结构 串:零个或多个字符组成的有限序列。 串长度:串中所包含的字符个数。 空串:长度为0的串,记为:" “。 非空串通常记为: S” s1 s2 …

输入npm run dev命令时出现 npm ERR! missing script: dev的bug

npm run dev npm ERR! missing script: devnpm ERR! A complete log of this run can be found in: npm ERR! /Users/ivyone/.npm/_logs/2020-10-12T07_24_45_379Z-debug.log答案:因为在某个文件中缺失start的有关dev的内容。把这个问题解决了就好了。

java路径两种写法“/“和“\\“

String path"D:\\新建文件夹\\2.png"; File filenew File(path); System.out.println(file.exists()); String path1"D:/新建文件夹/2.png"; File file1new File(path); System.out.println(file1.getAbsolutePath()); System.out.println(fil…

这些代码怎样run?

<!DOCTYPE html> <html> <head><title>Script 标签引入Demo</title><Script defer src"https://cdn.jsdelivr.net/npm/vue2.5.17/dist/vue.js"></Script> </head> <body><hi>Script 标签引入Demo</h…

SpringBoot打成jar包后无法读取resources资源文件里文件路径的问题 cannot be resolved to absolute file path because it does

SpringBoot打成jar包后无法读取resources资源文件 在项目中做了一个支付功能, 需要引入第三方渠道的配置文件config.xml用来初始化文件证书, 将配置文件 config.xml 放到 resources 资源目录下。 本地开发环境下能正常读取该文件, 但是在 Linux 环境下将项目打包成jar后运行会…