Laravel在2019年10月之后与Instagram API集成

时间:2019-12-02 15:50:11

标签: laravel instagram

自从Facebook更新其政策并强迫开发人员使用Instagram基本显示API:

https://developers.facebook.com/docs/instagram-basic-display-api/

如何将Laravel与Instagram API集成? 我研究了一下,所有教程都是关于https://socialiteproviders.netlify.com/providers/instagram.html的,并且已弃用。

我要做什么:

  1. 用户登录我们的广告系列
  2. 检查用户是否上传了带有特定标签的照片
  3. 如果是,则用户可以有效加入我们的广告系列

想法idea废,在这里提出问题。谢谢,无论如何有主意。

1 个答案:

答案 0 :(得分:0)

啊,最后,请遵循Facebook文档中的指南:

https://developers.facebook.com/docs/instagram-basic-display-api/getting-started

如果有人需要,这里是解决方案:

  1. 安装枪口
composer require guzzlehttp/guzzle
  1. 在.env中添加它
INSTAGRAM_CLIENT_ID=
INSTAGRAM_CLIENT_SECRET=
INSTAGRAM_REDIRECT_URI=https://yourdomainname.com/login/instagram/callback
  1. 将其添加到routes / web.php
Route::get('login/instagram',
 'Auth\LoginController@redirectToInstagramProvider')->name('instagram.login');

Route::get('login/instagram/callback', 'Auth\LoginController@instagramProviderCallback')->name('instagram.login.callback');
  1. 将其添加到config / services.php
'instagram' => [  
     'client_id' => env('INSTAGRAM_CLIENT_ID'),  
     'client_secret' => env('INSTAGRAM_CLIENT_SECRET'),  
     'redirect' => env('INSTAGRAM_REDIRECT_URI'),  
],
  1. 将其添加到Auth / LoginController.php
use GuzzleHttp\Client;

public function redirectToInstagramProvider()
{
    $appId = config('services.instagram.client_id');
    $redirectUri = urlencode(config('services.instagram.redirect'));
    return redirect()->to("https://api.instagram.com/oauth/authorize?app_id={$appId}&redirect_uri={$redirectUri}&scope=user_profile,user_media&response_type=code");
}

public function instagramProviderCallback(Request $request)
{
    $code = $request->code;
    if (empty($code)) return redirect()->route('home')->with('error', 'Failed to login with Instagram.');

    $appId = config('services.instagram.client_id');
    $secret = config('services.instagram.client_secret');
    $redirectUri = config('services.instagram.redirect');

    $client = new Client();

    // Get access token
    $response = $client->request('POST', 'https://api.instagram.com/oauth/access_token', [
        'form_params' => [
            'app_id' => $appId,
            'app_secret' => $secret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $redirectUri,
            'code' => $code,
        ]
    ]);

    if ($response->getStatusCode() != 200) {
        return redirect()->route('home')->with('error', 'Unauthorized login to Instagram.');
    }

    $content = $response->getBody()->getContents();
    $content = json_decode($content);

    $accessToken = $content->access_token;
    $userId = $content->user_id;

    // Get user info
    $response = $client->request('GET', "https://graph.instagram.com/me?fields=id,username,account_type&access_token={$accessToken}");

    $content = $response->getBody()->getContents();
    $oAuth = json_decode($content);

    // Get instagram user name 
    $username = $oAuth->username;

    // do your code here
}