Twitter API V2与Laravel 8 Usign Atymic集成

发布于 2025-02-10 18:34:43 字数 1699 浏览 1 评论 0 原文

我正在尝试使用Twitter API V2,但是每次在网站上运行时,我都会出现一个错误:

atymic \ twitter \ exception \ request \ unuthorizedRequestException 请求错误发生。客户端错误:获取https://api.twitter.com/2/tweets/searchs/search/recent?place.fields = country%2cname&tweet.fields = author_id%2cgeo&amp 导致 401未经授权响应:{“ title”:“未经授权”,“ type”:“ type”:“ of:abter”:“ abter”,“ status”,“ status”:401,“详细信息”:“未经授权” }

这是负责任的控制器。

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));
}

}

这是路由

Route::get('media/{query}',[mediacontroller::class,'searchRecent']);

,当运行时,您可以自己浏览并进行测试,其。媒体之后的接下来是查询。

所有代码的参考来自GitHub上的Atymic文档

noremic github文档a>

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 a 401 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.

enter image description here

The reference of all the code is from the atymic documentation on github

atymic github documentation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文