I am trying to use the Twitter API v2, but every time I do run on the site, I get an error:
Atymic\Twitter\Exception\Request\UnauthorizedRequestException A request error occurred. Client error:
GET https://api.twitter.com/2/tweets/search/recent?place.fields=country%2Cname&tweet.fields=author_id%2Cgeo&expansions=author_id%2Cin_reply_to_user_id&query=a
resulted in a401 Unauthorized
response: { "title": "Unauthorized", "type": "about:blank", "status": 401, "detail": "Unauthorized" }
Here is the responsible controller.
Mediacontroller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Atymic\Twitter\Twitter as TwitterContract;
use Illuminate\Http\JsonResponse;
use Twitter;
class mediacontroller extends Controller
{
public function searchRecent(string $query): JsonResponse
{
$params = [
'place.fields' => 'country,name',
'tweet.fields' => 'author_id,geo',
'expansions' => 'author_id,in_reply_to_user_id',
TwitterContract::KEY_RESPONSE_FORMAT => TwitterContract::RESPONSE_FORMAT_JSON,
];
return JsonResponse::fromJsonString(Twitter::searchRecent($query, $params));
}
}
And here is the Routing
Route::get('media/{query}',[mediacontroller::class,'searchRecent']);
And when run, the out come is, you can head over and test it yourself its https://mytry.studio/media/. what follows after the media/ is the query.
The reference of all the code is from the atymic documentation on github