【问题标题】:Instagram API Basic Display: Problem with requesting access_tokenInstagram API 基本显示:请求 access_token 时出现问题
【发布时间】:2019-11-28 15:38:35
【问题描述】:

我正在关注Instagram API Basic Display 文档。我已经创建了 Facebook 应用,配置了 Instagram 基本显示,添加了测试用户,使用 GET 请求验证了测试用户:

https://api.instagram.com/oauth/authorize
  ?app_id={app-id}
  &redirect_uri={redirect-uri}
  &scope=user_profile,user_media
  &response_type=code

但是当我尝试使用文档中的 POST 请求请求 access_token 时:我收到错误 400 并显示消息“您必须提供 client_id”。但是,文档没有说明 client_id,Instagram Basic Display 也没有提供 client_id。

我做错了什么?有没有人遇到过类似的问题?

【问题讨论】:

  • 为什么你的 POST 参数中有 中的 app_id? (App id 和 client id 应该是同一个东西,它们只是与 AFAIK 的措辞不一致。)
  • @04FS,这就是 Postman 的工作原理,当你输入查询参数时,它会在 URI 中显示它们。但我的问题是,为什么“Instagram API 基本显示”需要这个 client_id?如果您在 developers.facebook.com 创建 instagram 应用程序,则没有 client_id 并且如果您准备好他们的文档,它清楚地说只是发送 app_id 和 app_secret。
  • 正如我所说,应用 ID 和客户端 ID 都被 Facebook 用作同义词。
  • 您只是没有在此处正确发送 POST 参数。这些不应该在 Params 选项卡上输入(实际上创建查询字符串参数,而 that 是它们显示为 URL 的一部分的原因),而是在 Body 选项卡上输入,编码为 x-www-form-urlencoded
  • 谢谢 :) 你是对的!但是,现在我收到“”error_message“:“未找到匹配代码或已使用匹配代码”。我正在使用新生成的代码。现在我正试图找出原因。

标签: facebook api instagram access-token instagram-api


【解决方案1】:

您应该使用 body 中的参数而不是 Params 向 https://api.instagram.com/oauth/access_token 发出 POST 请求。确保启用“x-www-form-urlencoded”选项。

在此处查看更详细的答案:https://stackoverflow.com/a/60851414/1908112

【讨论】:

    【解决方案2】:

    我设法通过像这样使用 GuzzleHttp\Client 让我的工作。

    第 1 步。获取授权码$code

    第 2 步。获取短暂的 AccessToken

    短期访问令牌的有效期仅为 1 小时。

    $aAccessToken = $this->fetchAccessToken( $code );
    $short_lived_access_token = $aAccessToken[ 'access_token' ];
    $user_id                  = $aAccessToken[ 'user_id' ];
    

    第 3 步(可选)

    如果您想要有效期为 60 天的 Long-Lived 令牌,您可以立即兑换 $short_lived_access_token。

    $aLongLivedTokenResult   =           = $this->GetLongLivedToken( $short_lived_access_token );
    $long_lived_access_token = $aLongLivedTokenResult[ 'access_token' ];
    $expires_in = $aLongLivedTokenResult[ 'expires_in' ];
    

    long_lived_access_token 和 expires_in 可以保存,当令牌在 60 天后过期时,您可以刷新它。

    第 4 步 现在您可以像这样获取用户媒体。

    请记住,long_lived_access_token 已过期,在您 FETCH 之前,您应该实际检查令牌是否已过期,如果已过期,请交换它以获取新的。令牌回收开始。

        $aQueryString = [
            'fields'       => 'id,media_url,permalink,timestamp,caption',
            'access_token' => $long_lived_access_token,
    
        ];
        $uri = 'https://graph.instagram.com/{$user_id}/media?' . http_build_query( $aQueryString ) );
    

    //功能

    由于 fetchAccessToken 函数使用 POST 方法,因此仅在标头上添加 content-type = application/x-www-form-urlencoded 并没有真正起作用。 form_params 选项对我有用。

    private function fetchAccessToken(){
        $aOptions = [
          'app_id'       => $this->provider->AppID,
          'app_secret'   => $this->provider->AppSecret,
          'grant_type'   => 'authorization_code',
          'redirect_uri' => $this->provider->getRedirectUri(),
          'code'         => $accessCode,       
        ];
    
        $client   = new Client( [
            'base_uri' => 'https://api.instagram.com',
            'headers'  => [
                'content-type' => 'application/x-www-form-urlencoded',
            ],
        ] );
    
    
        $response = $client->request( 'POST', 'oauth/access_token', [
            'form_params' => $aOptions,
        ] );
        return json_decode( $response->getBody(), true );
    
    }
    
    private function GetLongLivedToken( $access_token )
    {
    
        $aOptions = [
            'grant_type'    => 'ig_exchange_token',
            'client_secret' => $this->provider->AppSecret,
            'access_token'  => $access_token,
    
        ];
    
        $response =  new Client( [
            'base_uri' => 'https://graph.instagram.com',
        ] )->request( 'GET', 'access_token?' . http_build_query( $aOptions ) );
    
        $stream   = $response->getBody();
        $contents = $stream->getContents();
        return json_decode( $contents, true ); 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多