首页 文章

如何获得Instagram集成authorization_code?

提问于
浏览
0

请帮助,如何获得authorization_code?

curl \-F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR-REDIRECT-URI' \
-F 'code=CODE'

https://api.instagram.com/oauth/access_token {"error_type":"OAuthException","code":400,"error_message":"Invalid grant_type"}

这很令人困惑 . 我需要USER_ID .

有人说,它的authorization_code是参数 . 但我不明白 . 什么是param在这里 .

1 回答

  • 0

    我知道,这是一个非常晚的回复,但我希望它会帮助其他人:

    PHP代码:

    步骤1:使用client_id和重定向域获取代码,您将获得响应代码 . 它将使用代码参数的查询字符串重定向到您的redirect_uri域 .

    https://api.instagram.com/oauth/authorize/?client_id=YOUR_CLIENT_ID&redirect_uri=http_domain_dot_com&response_type=code
    

    步骤2:发出POST请求以使用client_id,client_secret以及您从步骤1中使用redirect_uri收到的代码来获取access_token .

    我在这里使用PHP代码,例如:

    $post = ['client_id'=> "5b623cdffe6359694fcca2f772",
        'client_secret'=> "0fafe0a956dd1bfc81c74cdd9c",
        'grant_type'=> "authorization_code",
        'redirect_uri'=> "your domain using http",
        'code'=> "58f6905548f45c35623f5cd85"];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://api.instagram.com/oauth/access_token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $response = curl_exec($ch);
    $result = json_decode($response);
    
    var_dump($result);
    

    您将获得access_token作为响应 . 现在您可以使用https://api.instagram.com/v1/users/self/media/recent/?access_token=8626207.5b6cd.a45ae4d343329ed05429567966d9访问Feed

    现在,您可以通过读取json响应来打印Feed数据:

    $userId = 'self';
    $accessToken = '860000207.5b0000d.a4500003e004033200500950000600';
    $imageNumber = '6';
    $imageResolution = 'thumbnail'; // thumbnail, low_resolution, standard_resolution
    
    $url = 'https://api.instagram.com/v1/users/' . $userId . '/media/recent/?access_token=' . $accessToken.'&count='.$imageNumber;
    $args = stream_context_create(array('http' => array('timeout' => 2500,)));
    $instagramFeedsData = file_get_contents($url, false, $args);
    $instagramFeeds = json_decode($instagramFeedsData);
    
    $instangramData = $instagramFeeds->data;
    foreach ($instangramData as $instagramFeed) {
    $instagramImage = $instagramFeed->images->$imageResolution->url;
    $caption_text = 'Instagram';
    if (is_object($instagramFeed->caption)) {
        $caption_text = $instagramFeed->caption->text;
    }
    ?>
    <div class="instangram-feed">
        <a href ="<?php echo $instagramFeed->link; ?>" target="_blank">
            <img src="<?php echo $instagramImage; ?>" title="<?php echo $caption_text; ?>" alt="<?php echo $caption_text; ?>"/>
        </a>               
    </div>
    <?php } ?>
    

相关问题