php Tiktok oauth请求参数格式不正确

byqmnocz  于 2023-06-28  发布在  PHP
关注(0)|答案(2)|浏览(188)

我正在尝试将我的授权令牌换成不记名令牌。According to the docs应该是application/x-www-form-urlencoded请求。我的代码看起来像这样:

$res = Http::withHeaders([
    'Accept'       => 'application/json',
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Cache-Control' => 'no-cache'
])->post('https://open.tiktokapis.com/v2/oauth/token/', [
    'client_id'    => 'my-client-id',
    'client_secret' => 'my-client-secret',
    'code'          => $request->code,
    'grant_type'    => 'authorization_code',
    'redirect_uri'  => 'https://example.com/callback/tiktok',
]);

我不断收到:

{"error":"invalid_request","error_description":"The request parameters are malformed.","log_id":"20230621065239FB74CE96D69DA40A2B46"}

这到底是怎么回事一周前就联系过tiktok了,但没有回应。

umuewwlo

umuewwlo1#

Illuminate\Support\Facades\Http facade似乎对Illuminate\Http\Client\PendingRequest类的内部受保护变量$bodyFormat产生了一些问题,因为它在发出请求时在内部创建了该类的示例。
您可以直接使用PendingRequest类来发出如下请求:

片段:

<?php

use Illuminate\Http\Client\PendingRequest; 

$o = new PendingRequest();
$o->asForm(); // to set content type header to application/x-www-form-urlencoded

$res = $o->post('https://open.tiktokapis.com/v2/oauth/token/', [
  'client_key'    => 'CLIENT_KEY', // your value here
  'client_secret' => 'CLIENT_SECRET', // your value here
  'code'          =>  'CODE', // your value here
  'grant_type'    => 'authorization_code',
  'redirect_uri'  => 'https://example.com/callback/tiktok' // your value here
]);

dd($res->body());

在线演示

enxuqcxy

enxuqcxy2#

我之前也遇到过同样的问题。我必须对身体参数进行编码,之后,它就起作用了。PHP不是我的第一语言,但也许你可以尝试这样的东西:

$res = Http::withHeaders([
    'Accept'       => 'application/json',
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Cache-Control' => 'no-cache'
])->post('https://open.tiktokapis.com/v2/oauth/token/', http_build_query([
    'client_key'    => 'my-client-id',
    'client_secret' => 'my-client-secret',
    'code'          => $request->code,
    'grant_type'    => 'authorization_code',
    'redirect_uri'  => 'https://example.com/callback/tiktok',
]));

相关问题