Instagram基本显示API withlaravel

发布于 2025-02-05 18:25:49 字数 910 浏览 2 评论 0原文

我正在研究Instagram Basic API,在我的项目中,用户可以将其Instagram feed连接到我们的项目。为此,我正在生成一个简短的实时身份验证代码,但无法获得长时间的实时访问代码。它返回的空。

这是我获得长时间实时访问代码的代码。

    $client_secret = "###############"; 
    $client_id =  "###############";
    $redirect_uri = '#########'; 
    $short_access_token = "####"; // getting this from another function

  $ig_rtu = 'https://api.instagram.com/oauth/access_token?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&redirect_uri='.$redirect_uri.'&access_token='.$short_access_token;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $ig_rtu);
    
    curl_setopt($ch, CURLOPT_HEADER, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $ig_new = curl_exec($ch);

    curl_close ($ch);

    return $ig_new = json_decode($ig_new, true);

在Postman中,这可以正常工作,但我无法在Laravel完成。 我在这里想念什么? 任何铅都值得赞赏。

I'm working on Instagram basic api, in my project user can connect their Instagram feed to our project. For that I'm generating a short live authentication code but could not get the long live access code. Its returning null.

Here is my code for getting long live access code.

    $client_secret = "###############"; 
    $client_id =  "###############";
    $redirect_uri = '#########'; 
    $short_access_token = "####"; // getting this from another function

  $ig_rtu = 'https://api.instagram.com/oauth/access_token?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&redirect_uri='.$redirect_uri.'&access_token='.$short_access_token;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $ig_rtu);
    
    curl_setopt($ch, CURLOPT_HEADER, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $ig_new = curl_exec($ch);

    curl_close ($ch);

    return $ig_new = json_decode($ig_new, true);

In postman this works fine but I could not get it done in Laravel.
What I'm missing here ?
Any lead is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-02-12 18:25:49

我已经完成了3个步骤。 删除代码,并在步骤2和3中包括

  1. 我将在第1步中

实现

public $appId       = null;
public $appSecret   = null;
public $redirectUrl = null;

// Instagram request data
public $socialUserId                            = null;
public $socialAuthCode                          = null;
public $socialUserName                          = null;
public $socialShortAccessToken                  = null; // 1 hour
public $socialLongAccessToken                   = null; // 60 days
public $socialLongAccessTokenExpiresInSeconds   = null; // 5183910 seconds || 59.99896 days - relative to date created

步骤2:

public function getUserIdAndShortAccessToken(){

    /* Get UserId and access Token
      https://api.instagram.com/oauth/access_token \
      -F client_id={app-id} \
      -F client_secret={app-secret} \
      -F grant_type=authorization_code \
      -F redirect_uri={redirect-uri} \
      -F code={code}
    */

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 
    'https://api.instagram.com/oauth/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $post = array(
        'client_id'     => $this->appId,
        'client_secret' => $this->appSecret,
        'grant_type'    => 'authorization_code',
        'redirect_uri'  => $this->redirectUrl,
        'code'          => $this->socialAuthCode,
    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $curlResult = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
        curl_close($ch);
        return;
    }else{
        curl_close($ch);
    }

    $this->socialUserId = json_decode($curlResult)->user_id;
    $this->socialShortAccessToken = json_decode($curlResult)->access_token;
}

步骤3:

public function getLongAccessToken(){

        /* Get long-lived token
            curl -i -X GET "https://graph.instagram.com/access_token
            ?grant_type=ig_exchange_token
            &client_secret={instagram-app-secret}
            &access_token={short-lived-access-token}"
        */

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret='
            .$this->appSecret.'&access_token='
            .$this->socialShortAccessToken
        );

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $curlResult = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
            curl_close($ch);
            return;
        }else{
            curl_close($ch);
        }

        $this->socialLongAccessToken = json_decode($curlResult)->access_token;
        $this->socialLongAccessTokenExpiresInSecond = json_decode($curlResult)->expires_in;

}

I've done this in 3 steps. I will leave the code out for step 1 and include my implementation for step 2 and 3.

Steps:

  1. get the code
  2. get the short lived token using the code
  3. get the long lived token using the short lived token

Here are the public variables used.

public $appId       = null;
public $appSecret   = null;
public $redirectUrl = null;

// Instagram request data
public $socialUserId                            = null;
public $socialAuthCode                          = null;
public $socialUserName                          = null;
public $socialShortAccessToken                  = null; // 1 hour
public $socialLongAccessToken                   = null; // 60 days
public $socialLongAccessTokenExpiresInSeconds   = null; // 5183910 seconds || 59.99896 days - relative to date created

Step 2:

public function getUserIdAndShortAccessToken(){

    /* Get UserId and access Token
      https://api.instagram.com/oauth/access_token \
      -F client_id={app-id} \
      -F client_secret={app-secret} \
      -F grant_type=authorization_code \
      -F redirect_uri={redirect-uri} \
      -F code={code}
    */

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 
    'https://api.instagram.com/oauth/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    $post = array(
        'client_id'     => $this->appId,
        'client_secret' => $this->appSecret,
        'grant_type'    => 'authorization_code',
        'redirect_uri'  => $this->redirectUrl,
        'code'          => $this->socialAuthCode,
    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $curlResult = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
        curl_close($ch);
        return;
    }else{
        curl_close($ch);
    }

    $this->socialUserId = json_decode($curlResult)->user_id;
    $this->socialShortAccessToken = json_decode($curlResult)->access_token;
}

Step 3:

public function getLongAccessToken(){

        /* Get long-lived token
            curl -i -X GET "https://graph.instagram.com/access_token
            ?grant_type=ig_exchange_token
            &client_secret={instagram-app-secret}
            &access_token={short-lived-access-token}"
        */

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret='
            .$this->appSecret.'&access_token='
            .$this->socialShortAccessToken
        );

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $curlResult = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
            curl_close($ch);
            return;
        }else{
            curl_close($ch);
        }

        $this->socialLongAccessToken = json_decode($curlResult)->access_token;
        $this->socialLongAccessTokenExpiresInSecond = json_decode($curlResult)->expires_in;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文