简体   繁体   中英

getting the users instagram posts api in laravel php

i simply want to take users post in my laravel application . 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 :

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

then i generates an User Token and a App Token after that i called this 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 :

{
    "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.
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:

 '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.
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.
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.

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/入门

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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