如何实现instagram基本显示api的instagram或facebook登录按钮的继续

问题描述

我要使用Instagram登录到我的网站。 该网站将使用用户的Instagram帐户控制Instagram媒体。网站访问者是消费者(即非企业或非创建者) 因此,我应该使用Instagram基本显示API(不能使用Instagram图形API,因为这需要用户成为公司或创建者。)

我尝试实现“使用Instagram登录”,但是developer guideInstagram Graph API cannot access Instagram consumer accounts (i.e.,non-Business or non-Creator Instagram accounts). If you are building an app for consumer users,use the Instagram Basic Display API instead. Instagram Basic Display is not an authentication solution. Data returned by the API cannot be used to authenticate your app users or log them into your app. If you need an authentication solution we recommend using Facebook Login instead.

因此,我单击了“使用Facebook登录”按钮,但是问题是,如果我使用Instagram范围的Facebook登录,则要求我在登录时创建Instagram商业帐户。我回到了第一位置。 什么循环?有什么解决方案?我对指南的理解不正确吗?

解决方法

我们为什么不使用在Upwork上回复的laravel socialite软件包呢? https://socialiteproviders.com/Instagram/#installation-basic-usage

,

也许您可以使用这种方法

  1. 您询问他们的用户名(带有消​​息:如果输入您的用户名,则表示您同意我们的政策)
  2. 您可以通过此链接https://www.instagram.com/{username}/?__a=1
  3. 访问他们的instagram数据
  4. 如果数据库中存在用户名,则登录;如果新用户,则注册。

评论后进行编辑

无需首先使用库,您必须遵循此链接https://developers.facebook.com/docs/instagram-basic-display-api/getting-started

上的说明

仅供参考,我使用Laravel创建了这个简单的应用https://youtu.be/Qhg9vfB68J8?t=9

我的控制器看起来像这样

public function redirectToInstagramProvider()
{
    $appId       = "1564152940449991"; // change this to your appId
    $secret      = "f9711d1d48797fa30d4b2055ae5734f9"; // change this to your secret
    $redirectUri = "https://newgli.test/login/instagram/callback"; // change this to your redirectUri

    $redirectUri = urlencode($redirectUri);
    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       = "1564152940449991"; // change this to your appId
    $secret      = "f9711d1d48797fa30d4b2055ae5734f9"; // change this to your secret
    $redirectUri = "https://newgli.test/login/instagram/callback"; // change this to your redirectUri

    $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);
    dd($oAuth);
    // Get instagram user name
    $username = $oAuth->username;

    // do your code here
}

我知道代码不干净。您调整代码以清理它。也许您可以将appId,Secret,redirectUrl移到配置文件或.env文件中

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...