英文:
Oauth: problem of getting access token from TikTok
问题
We are implementing "Log in with Tiktok" on our website. We are able to redirect users to Tiktok and get user initial authorization according to the instructions on this link:
https://developers.tiktok.com/doc/login-kit-web/
However, when we follow the instructions on this TikTok page to get access token:
https://developers.tiktok.com/doc/login-kit-manage-user-access-tokens
we always get this error:
{"data":{"captcha":"","desc_url":"","description":"Enter the correct parameter","error_code":10002},"message":"error"}
We are using Java and this is our code:
URIBuilder builder = new URIBuilder("https://open-api.tiktok.com/oauth/access_token/");
HttpPost post = new HttpPost(builder.build());
post.setHeader("code", code);
post.setHeader("grant_type", "authorization_code");
post.setHeader("client_key", "client key goes here"));
post.setHeader("client_secret", "client secret goes here");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(post);
We found this related link at SO:
https://stackoverflow.com/questions/74747343/tiktok-oauth-parameter-error-login-kit
but have no idea of how to do it in Java. Anybody knows the correct way to obtain access token from TikTok in Java?
英文:
We are implementing "Log in with Tiktok" on our website. We are able to redirect users to Tiktok and get user initial authorization according to the instructions on this link:
https://developers.tiktok.com/doc/login-kit-web/
However, when we follow the instructions on this TikTok page to get access token
https://developers.tiktok.com/doc/login-kit-manage-user-access-tokens
we always get this error:
{"data":{"captcha":"","desc_url":"","description":"Enter the correct parameter","error_code":10002},"message":"error"}
We are using Java and this is our code:
URIBuilder builder = new URIBuilder("https://open-api.tiktok.com/oauth/access_token/");
HttpPost post = new HttpPost(builder.build());
post.setHeader("code", code);
post.setHeader("grant_type", "authorization_code");
post.setHeader("client_key", "client key goes here"));
post.setHeader("client_secret", "client secret goes here");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(post);
We found this related link at SO
https://stackoverflow.com/questions/74747343/tiktok-oauth-parameter-error-login-kit
but have no idea of how to do it in Java. Anybody knows the correct way to obtain access token from TikTok in Java?
答案1
得分: 1
我不太了解Java,但我认为你遇到的第一个问题是因为你在头部设置了:
post.setHeader("code", code);
相反,你应该在请求体参数中进行设置。
但是,你的端点并没有被弃用。TikTok更新了授权的端点。获取访问令牌的新端点是:
https://open.tiktokapis.com/v2/oauth/token/
他们还添加了一个额外的参数,除了你已经有的参数,你还需要在请求体参数中设置redirect_uri。
参考链接:
https://developers.tiktok.com/doc/oauth-user-access-token-management
英文:
I don't understand a lot of Java, but I think the first issue you have is because you're setting in the header:
post.setHeader("code", code);
Instead, you should be setting them in the request body params.
But also, your endpoint is not deprecated. TikTok updated the endpoints for authorization. The new endpoint to retrieve the access token is:
https://open.tiktokapis.com/v2/oauth/token/
they also added an extra param beside the ones you have and so you also have to set the redirect_uri in the body params.
Reference:
https://developers.tiktok.com/doc/oauth-user-access-token-management
答案2
得分: 1
以下是您要翻译的内容:
根据Karine的回答,首先要在标题中设置参数,如下所示:
post.setHeader("code", code);
post.setHeader("grant_type", "authorization_code");
post.setHeader("client_key", "client key goes here");
post.setHeader("client_secret", "client secret goes here");
相反,您应该将它们设置为x-www-form-urlencoded请求正文。
此外,请注意,TikTok已经更改了API,现在是POST https://open.tiktokapis.com/v2/oauth/token/
// 更新的URL
URIBuilder builder = new URIBuilder("https://open.tiktokapis.com/v2/oauth/token/");
HttpPost post = new HttpPost(builder.build());
// 创建表单参数的列表
List<NameValuePair> urlParameters = new ArrayList<>();
// 添加表单数据
urlParameters.add(new BasicNameValuePair("code", code));
urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
urlParameters.add(new BasicNameValuePair("client_key", "client key goes here"));
urlParameters.add(new BasicNameValuePair("client_secret", "client secret goes here"));
urlParameters.add(new BasicNameValuePair("redirect_uri", "client key goes here"));
// 在HttpPost中设置表单数据
post.setEntity(new UrlEncodedFormEntity(urlParameters));
// 将Content-Type标题设置为“application/x-www-form-urlencoded”
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(post);
如果我有任何错误,请随时更正。
英文:
As answered by Karine, you are firstly setting the parameters in headers like this :
post.setHeader("code", code);
post.setHeader("grant_type", "authorization_code");
post.setHeader("client_key", "client key goes here"));
post.setHeader("client_secret", "client secret goes here");
Instead, you should set them as x-www-form-urlencoded request body.
Also, note that the API has been changed by TikTok which is POST https://open.tiktokapis.com/v2/oauth/token/
// updated URL
URIBuilder builder = new URIBuilder("https://open.tiktokapis.com/v2/oauth/token/");
HttpPost post = new HttpPost(builder.build());
// create list for form parameters
List<NameValuePair> urlParameters = new ArrayList<>();
// add form data
urlParameters.add(new BasicNameValuePair("code", code);
urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code");
urlParameters.add(new BasicNameValuePair("client_key", "client key goes here");
urlParameters.add(new BasicNameValuePair("client_secret", "client secret goes here");
urlParameters.add(new BasicNameValuePair("redirect_uri", "client key goes here");
// set form data in HttpPost
post.setEntity(new UrlEncodedFormEntity(urlParameters));
// set Content-Type header as "application/x-www-form-urlencoded"
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(post);
Feel free to correct me if I am wrong somewhere.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论