一、在yml中添加抖音开放平台的账号基本信息
# 抖音开放平台配置信息
dy:
clientKey: #抖音开放平台key
clientSecret: #抖音开放平台密钥
scope: data.external.user,data.external.item,fans.data,user_info,renew_refresh_token,data.external.user#需要用户开放的权限
responseType: code#填写code就行
redirectUri: #扫码之后的回调地址
state: false
二、设计config文件将抖音数据注入
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "dy")
public class DouYinConfig {
private String clientKey;
private String clientSecret;
private String responseType;
private String scope;
private String redirectUri;
private String state;
public String getClientKey() {
return clientKey;
}
public void setClientKey(String clientKey) {
this.clientKey = clientKey;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getResponseType() {
return responseType;
}
public void setResponseType(String responseType) {
this.responseType = responseType;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getRedirectUri() {
return redirectUri;
}
public void setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
三、二维码url拼接
Api
@Resource
private DouYinService douYinService;
@GetMapping("code")
public void Code(HttpServletRequest request, HttpServletResponse response) throws IOException {
String requestUrl = douYinService.qrcodeAuth();
System.out.println("回调url:"+requestUrl);
response.sendRedirect(requestUrl);
}
service
@Autowired
DouYinConfig douYinCode;
Logger logger = LoggerFactory.getLogger(getClass());
public String qrcodeAuth() {
String code = "https://open.douyin.com/platform/oauth/connect/?client_key=" + douYinCode.getClientKey()
+ "&response_type="+douYinCode.getResponseType()+"&scope="+douYinCode.getScope()+"&redirect_uri="+douYinCode.getRedirectUri()+"&state="+douYinCode.getState();
logger.info("qrConnect requestUrl=" + code);
return code;
}
四、访问url效果如下
注意:如果手机版抖音扫码出现重定向失败,就在回调地址之前添加http://
五、其他的授权调用都是通过扫码后得到的code(每次扫码得到一个code,每个code只能使用一次),之后使用code得到token,使用得到token中的信息实现授权功能的数据爬取。
六、得到token的url:
String token_url = "https://open.douyin.com/oauth/access_token/?client_key=" + clientKey + "&client_secret=" + clientSecret + "&code="+code+"&grant_type=authorization_code";
七、其他的授权后的数据爬取参考
https://open.douyin.com/platform/doc/6848806527751489550