获取TikTok的访问令牌的问题

huangapple go评论74阅读模式
英文:

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(&quot;code&quot;, code);
post.setHeader(&quot;grant_type&quot;, &quot;authorization_code&quot;);         
post.setHeader(&quot;client_key&quot;, &quot;client key goes here&quot;));
post.setHeader(&quot;client_secret&quot;, &quot;client secret goes here&quot;);

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(&quot;https://open.tiktokapis.com/v2/oauth/token/&quot;);         
HttpPost post = new HttpPost(builder.build());

// create list for form parameters
List&lt;NameValuePair&gt; urlParameters = new ArrayList&lt;&gt;();

// add form data
urlParameters.add(new BasicNameValuePair(&quot;code&quot;, code);
urlParameters.add(new BasicNameValuePair(&quot;grant_type&quot;, &quot;authorization_code&quot;);
urlParameters.add(new BasicNameValuePair(&quot;client_key&quot;, &quot;client key goes here&quot;);
urlParameters.add(new BasicNameValuePair(&quot;client_secret&quot;, &quot;client secret goes here&quot;);
urlParameters.add(new BasicNameValuePair(&quot;redirect_uri&quot;, &quot;client key goes here&quot;);

// set form data in HttpPost
post.setEntity(new UrlEncodedFormEntity(urlParameters));

// set Content-Type header as &quot;application/x-www-form-urlencoded&quot;
post.setHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse httpResponse = httpclient.execute(post);

Feel free to correct me if I am wrong somewhere.

huangapple
  • 本文由 发表于 2023年5月7日 07:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定