官网地址点击
写这个目的是为了防止过期 (调用 菜品识别看我其他文章)
(每次菜品识别都获取token防止失效,也可以存到redis里面,从redis取值 定时30天获取新的防止失效)
postman 获取
java代码获取
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
class Sample {
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token?client_id=&client_secret=&grant_type=client_credentials")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
System.out.println(response.body().string());
}
}
修改代码
https://aip.baidubce.com/oauth/2.0/token?client_id=&client_secret=&grant_type=client_credentials
client_id 对应的是 API Key
client_secret对应的是 Secret Key
最终这个代码 是
https://aip.baidubce.com/oauth/2.0/token?client_id=获取的API Key&client_secret=获取的Secret Key&grant_type=client_credentials
最终java 代码
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
class Sample {
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token?client_id=获取的API Key&client_secret=获取的Secret Key&grant_type=client_credentials")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
System.out.println(response.body().string());
}
}