1.用这个url去换取code
https://open.douyin.com/platform/oauth/connect?client_key=【你的client_key】&response_type=code&scope=trial.whitelist&redirect_uri=https%3A%2F%2Fwww.douyin.com
或
https://open.douyin.com/platform/oauth/connect?client_key=awq7clitdigv2b8c&response_type=code&scope=trial.whitelist&redirect_uri=https://www.douyin.com
2.上面会跳转到这个url
https://www.douyin.com/?code=【是你的code,会根据你的请求改变】&scopes=trial.whitelist&state=&recommend=1
3.把code调用授权接口即可绑定白名单
import com.aliyun.tea.TeaException;
import com.douyin.openapi.client.Client;
import com.douyin.openapi.client.models.OauthAccessTokenRequest;
import com.douyin.openapi.client.models.OauthAccessTokenResponse;
import com.douyin.openapi.credential.models.Config;
import com.istudy.meizhen.api.matrix.config.DouYinConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* @author 落尘
* @since 2024-11-02 14:03:06
*/
@SpringBootTest
@Slf4j
class DouYinServiceTest {
@Autowired
private DouYinService douYinService;
@Autowired
DouYinConfig douYinCode;
@Test
void getCode() {
try {
Config config = new Config().setClientKey(douYinCode.getClientKey()).setClientSecret(douYinCode.getClientSecret()); // 改成自己的app_id跟secret
Client client = new Client(config);
/* 构建请求参数,该代码示例中只给出部分参数,请用户根据需要自行构建参数值
token:
1.若用户自行维护token,将用户维护的token赋值给该参数即可
2.SDK包中有获取token的函数,请根据接口path在《OpenAPI SDK 总览》文档中查找获取token函数的名字
在使用过程中,请注意token互刷问题
header:
sdk中默认填充content-type请求头,若不需要填充除content-type之外的请求头,删除该参数即可
*/
OauthAccessTokenRequest sdkRequest = new OauthAccessTokenRequest();
sdkRequest.setClientKey(douYinCode.getClientKey());
sdkRequest.setClientSecret(douYinCode.getClientSecret());
sdkRequest.setCode("da35fc4e6106813er3r8k6MKkA8sLwJgwnrS_lq");
sdkRequest.setGrantType("authorization_code");
OauthAccessTokenResponse sdkResponse = client.OauthAccessToken(sdkRequest);
log.info("sdkResponse={}", sdkResponse.getData());
} catch (Exception e) {
log.error("error={}", e.getMessage());
}
}
/**
* 转码
*/
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(URLEncoder.encode("https://www.douyin.com/user/self", "UTF-8"));
}
}
4.常见问题
1)redirect_uri没有转码或者有特殊字符导致页面提示重定向地址非法
2)有可能没配置回调地址(最容易忽略)