简体   繁体   English

在laravel php中获取用户instagram帖子api

[英]getting the users instagram posts api in laravel php

i simply want to take users post in my laravel application .我只是想让用户在我的 Laravel 应用程序中发帖。 its even fine if my user has to insert his username password because i want to crawl all the posts media and text once and then they can change password .如果我的用户必须插入他的用户名密码,它甚至可以,因为我想一次抓取所有帖子媒体和文本,然后他们可以更改密码。 for the matter of what i tried so far : 1-i created an app in this link :对于我到目前为止所尝试的问题:1-我在此链接中创建了一个应用程序:

https://developers.facebook.com/apps/

then i generates an User Token and a App Token after that i called this api :然后我生成一个User Token和一个App Token之后我调用了这个 api:

https://graph.instagram.com/17841405793187218?fields=id,username&access_token=myAppToken

i placed my App token in that api but this showed me this error :我将我的 App 令牌放在那个 api 中,但这向我显示了这个错误:

{
    "error": {
        "message": "Access token does not contain a valid app ID",
        "type": "OAuthException",
        "code": 190,
        "fbtrace_id": "Agc0R_wnDwoCzgNuXt2gt1y"
    }
}

now i dont know even if i am on a right train or not but simply i want to crawl all users posts .现在我不知道我是否在正确的火车上,但我只想抓取所有用户的帖子。 thanks in advance提前致谢

You should use Laravel Socialite since it will simplify the whole process.你应该使用 Laravel Socialite,因为它会简化整个过程。
Follow the installation instructions here https://laravel.com/docs/8.x/socialite Don't forget to put the fb credentials in your config/service.php like so:按照此处的安装说明进行操作https://laravel.com/docs/8.x/socialite不要忘记将 fb 凭据放在config/service.php如下所示:

 'facebook'        => [
        'client_id'     => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect'      => env('FACEBOOK_REDIRECT_URL'),
    ],

Now that we have set up Socialite to work with Facebook, we should redirect the user to the facebook login page where it will enter his credentials.现在我们已经设置了 Socialite 与 Facebook 一起工作,我们应该将用户重定向到 Facebook 登录页面,在那里它将输入他的凭据。
Define the login route:定义登录路径:

 Route::get('facebook', 'FacebookLoginController@redirectToProvider');

Define the controller and the method:定义控制器和方法:

class FacebookLoginController extends Controller
{
    /**
     * Redirect the user to the FB authentication page.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function redirectToProvider(RedirectToProviderRequest $request)
    {
        return Socialite::driver('facebook')->scopes(['instagram_basic'])->asPopup()->usingGraphVersion('v7.0')->redirect();
    }
}

Once the user has enter his credentials and allowed your app to have access to his data, we need to grab the access token to make calls to the fb api on his behalf.一旦用户输入了他的凭据并允许您的应用访问他的数据,我们需要获取访问令牌以代表他调用 fb api。
Define the callback route in the same controller:在同一个控制器中定义回调路由:

Route::get('facebook/callback','FacebookLoginController@handleProviderCallback');

Define the callback method in the controller:在控制器中定义回调方法:

 public function handleProviderCallback(Request $request)
    {
        if ($request->input('error') == "access_denied")
        {
            return 'user has decline the authorization';
        } 
       
            $user = Socialite::driver('facebook')->usingGraphVersion('v7.0')->user();
            $token = $user->token;
            $refreshToken = $user->refreshToken;
            $id = $user->getId();
    }

From here, your have your access token ( $token ) to call the fb api.从这里开始,您可以使用访问令牌 ( $token ) 来调用 fb api。

Please note that the user can cancel the authorization, so will probably want to catch the error in the callback.请注意,用户可以取消授权,因此可能希望在回调中捕获错误。 I didn't put all the error handling stuff since it can complicate the code我没有把所有错误处理的东西都放在里面,因为它会使代码复杂化

查看文档,我认为应该使用应用程序密钥来生成访问令牌,请查看此步骤:第 5步:在文档中交换代码以获取令牌https : //developers.facebook.com/docs/instagram -basic-display-api/入门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM