自从Facebook更新其政策并强迫开发人员使用Instagram基本显示API:
https://developers.facebook.com/docs/instagram-basic-display-api/
如何将Laravel与Instagram API集成? 我研究了一下,所有教程都是关于https://socialiteproviders.netlify.com/providers/instagram.html的,并且已弃用。
我要做什么:
想法idea废,在这里提出问题。谢谢,无论如何有主意。
答案 0 :(得分:0)
啊,最后,请遵循Facebook文档中的指南:
https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
如果有人需要,这里是解决方案:
composer require guzzlehttp/guzzle
INSTAGRAM_CLIENT_ID=
INSTAGRAM_CLIENT_SECRET=
INSTAGRAM_REDIRECT_URI=https://yourdomainname.com/login/instagram/callback
Route::get('login/instagram',
'Auth\LoginController@redirectToInstagramProvider')->name('instagram.login');
Route::get('login/instagram/callback', 'Auth\LoginController@instagramProviderCallback')->name('instagram.login.callback');
'instagram' => [
'client_id' => env('INSTAGRAM_CLIENT_ID'),
'client_secret' => env('INSTAGRAM_CLIENT_SECRET'),
'redirect' => env('INSTAGRAM_REDIRECT_URI'),
],
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
}