I’m trying to exchange my authorization token for a bearer token. According to the docs it should be a application/x-www-form-urlencoded
request. My code looks like this:
$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',
]);
I keep receiving:
{"error":"invalid_request","error_description":"The request parameters are malformed.","log_id":"20230621065239FB74CE96D69DA40A2B46"}
What could be going on here? Already tried contacting tiktok a week ago but no response.
2
Answers
IlluminateSupportFacadesHttp
facade seems to create some problems with the internal protected variable$bodyFormat
ofIlluminateHttpClientPendingRequest
class as it creates the instance of this class internally while making requests.You could rather directly use
PendingRequest
class to make requests like below:Snippet:
Online Demo
I was facing the same issue earlier. I had to encode the body parameters, and after that, it worked. PHP is not my first language, but maybe you can try something like this: