0

I am making a post request to the tiktok API, entering the 3 parameters that are requested, but when I carry out the request, it shows me the following: "Required fields are missing: app_id is required.", I attach an image:

enter image description here

but when performing the same procedure in postman if I have a positive response.

here is my code:

my route:

Route::get('callback-tiktok', [AuthsController::class, 'SocialAuth']);

my controller:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class AuthsController extends Controller
{

    public function SocialAuth(Request $request)
    {
        $a = $request->input('auth_code');
        // create a guzzle client object here
        $client = new Client();
        $respuesta = $this->client->request(
            'POST',
            'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/',
            [
                'form_params' => [
                    'app_id' => '7112335319877287937',
                    'secret' => '18f52730856f43ed821187bfa9283794ca360ef1',
                    'auth_code' => $a
                ],
                'headers'  =>  [
                    'Content-Type'      =>  'application /json'
                ],
            ]
        );
        return response()->json($respuesta->getBody()->getContents());
    }
}

When compiling I get the following:

The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file "/home/epgutp/tiktok/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: The stream or file....

Julio
  • 29
  • 5

2 Answers2

1

I guess, you made some couple of errors, try this:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Http\Request;

class AuthsController extends Controller
{
    public function SocialAuth(Request $request)
    {
        $a = $request->input('auth_code');
        // create a guzzle client object here
        $client = new Client();
        $respuesta = $this->client->request('POST', 
              'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/',
             [
              'form_params' => [
                'app_id' => '711233531987728793',
                'secret' => '18f52730856f43ed821187bfa9283794ca360ef',
                'auth_code' => $a
              ], 
              'headers'  =>  [
                'Content-Type'      =>  'application /json'
              ],
          ]);
         return response()->json($respuesta->getBody()->getContents());
        
    }

}

Applying this solution gives me the following error:

enter image description here

Ibrahim Hammed
  • 833
  • 1
  • 8
  • 17
  • Thanks for the support, I edited the question by adding the CURL code provided by the tiktok documentation regarding authorization, I hope you can help me. – Julio Nov 24 '22 at 00:01
  • Well, the next is to troubleshoot the authentication as i said in the comment. Like remove the `secret` and `auth_code` and put it in the `headers`. or better still, paste the `tiktok` url here in the comment and let me check it out. – Ibrahim Hammed Nov 24 '22 at 07:26
  • Okay, i guess the issue is the `dd($cuerpo)`, try return the variable @Julio – Ibrahim Hammed Nov 24 '22 at 11:29
  • Here is the url of the tiktok documentation, specifically the request we are talking about: https://ads.tiktok.com/marketing_api/docs?id=1739965703387137 – Julio Nov 25 '22 at 02:39
  • putting the auth code and secret in the header, I get the following error: "request body has an error: header Content-Type has unexpected value "application /json"" – Julio Nov 25 '22 at 02:41
  • I've checked the url and sees that it's normal to pass it, but i think the `dd();` thats causing your issue. check the answer modification @Julio – Ibrahim Hammed Nov 25 '22 at 09:51
  • I have made the changes to the modified answer, I already edited the question with your changes (you could review it) and the problem that I get. – Julio Nov 25 '22 at 12:53
  • then search for the error online. check this: https://stackoverflow.com/questions/23411520/how-to-fix-error-laravel-log-could-not-be-opened – Ibrahim Hammed Nov 25 '22 at 15:56
  • I was able to find a solution to my problem, thank you very much for the support :D. – Julio Nov 29 '22 at 02:38
  • I still can't cast a vote, I need more reputation he tells me :( – Julio Nov 29 '22 at 12:11
  • @Julio you can cast vote now, your answer is helpful to the community – Ibrahim Hammed Nov 29 '22 at 12:30
  • Ready and vote, if you have time and you can help me with this question I would appreciate it :D....I want to store a data of the response of this request in mysql here expand the question: https://stackoverflow.com/questions/74608566/how-to-save-data-from-a-request-to-mysql-in-laravel – Julio Nov 29 '22 at 12:50
1

Well, maybe this can help someone, solution to my question:

In this case use the Http facade:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AuthsController extends Controller
{
    

    public function SocialAuth(Request $request)
    {

        $a = $request->input('auth_code');

        // URL
        $apiURL = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';

        // POST Data
        $postInput = [
            'app_id' => '7112335319877287',
            'secret' => '18f52730856f43ed821187bfa9283794ca360',
            'auth_code' => $a
        ];

        // Headers
        $headers = [
            //...
        ];

        $response = Http::withHeaders($headers)->post($apiURL, $postInput);

        $statusCode = $response->getStatusCode();
        $responseBody = json_decode($response->getBody(), true);

        echo $statusCode;  // status code

        dd($responseBody); // body response


    }
}

Response to my request:

^ array:4 [▼
  "code" => 0
  "message" => "OK"
  "request_id" => "202211281314430102451411010AF4A"
  "data" => array:3 [▼
    "access_token" => "fbcaa610339b7aeb39eabf29346d06a4e7fe9"
    "advertiser_ids" => array:1 [▶]
    "scope" => array:18 [▶]
  ]
]
Julio
  • 29
  • 5