Java实现抖音开放平台二维码授权功能

一、在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
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值