Instagram API基本显示:请求access_token的问题 [英] Instagram API Basic Display: Problem with requesting access_token
问题描述
我正在关注
我做错了什么?你们中有人遇到过类似的问题吗?
我设法使用GuzzleHttp\Client这样使我的工作正常了。 p>
第1步。获取授权代码$ code
步骤2 。获取短期访问令牌
短期访问令牌的有效期仅为1小时。
$ aAccessToken = $ this-> fetchAccessToken($ code);
$ short_lived_access_token = $ aAccessToken [‘access_token’];
$ user_id = $ aAccessToken [‘user_id’];
第3步(可选)
如果要使用有效期为60天的令牌,可以立即交换$ 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会过期,因此在您抓取之前,您应该实际检查令牌是否已过期,如果已过期,请交换令牌以获取新的令牌。然后便开始了令牌回收。
$ aQueryString = [
fields => ‘id,media_url,永久链接,时间戳,标题’,
‘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-urlencode并不能真正起作用。选项上的 form_params 为我解决了问题。
私有函数fetchAccessToken(){
$ aOptions = [
'app_id'=> $ this->提供者-> AppID,
app_secret => $ this-> provider-> AppSecret,
grant_type => ‘authorization_code’,
‘redirect_uri’=> $ this-> provider-> getRedirectUri(),
'code'=> $ accessCode,
];
$ 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);
}
私有函数GetLongLivedToken($ access_token)
{
$ aOptions = [
'grant_type'=> ; ‘ig_exchange_token’,
‘client_secret’=> $ this-> provider-> AppSecret,
access_token => $ access_token,
];
$ response =新客户([
'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);
}
I was following Instagram API Basic Display documentation. I've created Facebook App, configured Instagram Basic Display, added Test User, Authenticated the Test User using GET request:
https://api.instagram.com/oauth/authorize
?app_id={app-id}
&redirect_uri={redirect-uri}
&scope=user_profile,user_media
&response_type=code
But when I try to request access_token using POST request from the documentation: I receive ERROR 400 with message "You must provide a client_id". However, documentation says nothing about client_id and Instagram Basic Display doesn't provide client_id.
What am I doing wrong? Has any of you had similar problem?
I managed to get mine working by using GuzzleHttp\Client like this.
Step 1. get the Authorization Code $code
Step 2. Get the short-lived AccessToken
Short-Lived Access tokens are valid for just 1 hour.
$aAccessToken = $this->fetchAccessToken( $code );
$short_lived_access_token = $aAccessToken[ 'access_token' ];
$user_id = $aAccessToken[ 'user_id' ];
Step 3 (optional)
If you want the Long-Lived token, which is valid for 60days, you can immediately exchange the $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 and expires_in can be saved and when the token has expired after 60 days you can refresh it.
Step 4 Now you can fetch the user media like this.
Bear in mind that the long_lived_access_token expires and before you FETCH you should actually check if the token has expired and if it has, exchange it to get a new one. And the token recycling begins.
$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 ) );
//functions
Because the fetchAccessToken function uses the POST method, Adding content-type = application/x-www-form-urlencoded on the headers alone didn't really work. form_params on the options did the trick for me.
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 );
}
这篇关于Instagram API基本显示:请求access_token的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!