Instagram APIでのフォロー
Webブラウザ上でinstagramユーザーをフォローするプログラムを作りたいと思っています。
参考にさせて頂いているサイト(http://syncer.jp/instagram-api-matome)
をみながら作っています。
しかしエラー等出てしまい、フォローができません。
$params = array(
"access_token" => $access_token,
"action" => "follow",
);
$params = http_build_query($params);
$header = array(
"Content-Type: text/plain",
"Content-Length: ".strlen($params)
);
$obj = json_decode(file_get_contents(
"https://api.instagram.com/v1/users/{$user_id}/relationship",
false,stream_context_create(array("http" => array(
"method" => "POST",
'header' => implode(PHP_EOL,$header),
"content" => $params,
'ignore_errors' => true
)))
));
ほぼ参考にさせていただいたサイトそのままなのですが、先に進めなくなってしまいました。
Manage Clients内(https://instagram.com/developer/clients/manage/)
でEnforce signed requestsのチェックは入れています。(実際に必要かどうかわかりませんが。)
サーバー環境
- PHP Version 5.6.3
- Apache/2.4.10
- テスト用サーバーXAMPP Windows版
エラー内容を表示させると、以下の通りでした。
array(13) {
[0]=>
string(24) "HTTP/1.1 400 BAD REQUEST"
[1]=>
string(59) "Cache-Control: private, no-cache, no-store, must-revalidate"
[2]=>
string(20) "Content-Language: en"
[3]=>
string(45) "Content-Type: application/json; charset=utf-8"
[4]=>
string(35) "Date: Tue, 07 Jul 2015 05:38:19 GMT"
[5]=>
string(38) "Expires: Sat, 01 Jan 2000 00:00:00 GMT"
[6]=>
string(16) "Pragma: no-cache"
[7]=>
string(119) "Set-Cookie: csrftoken=トークン; expires=Tue, 05-Jul-2016 05:38:19 GMT; Max-Age=31449600; Path=/"
[8]=>
string(29) "Vary: Cookie, Accept-Language"
[9]=>
string(23) "X-Ratelimit-Limit: 5000"
[10]=>
string(27) "X-Ratelimit-Remaining: 4999"
[11]=>
string(19) "Content-Length: 282"
[12]=>
string(17) "Connection: Close"
}
object(stdClass)#3 (1) {
["meta"]=>
object(stdClass)#4 (3) {
["error_type"]=>
string(25) "OAuthPermissionsException"
["code"]=>
int(400)
["error_message"]=>
string(201) "This request requires scope=relationships, but this access token is not authorized with this scope. The user must re-authorize your application with scope=relationships to be granted write permissions."
}
}
アクセストークンの取得など、同じPOST通信は出来ている状況です。
また以下の通り、apigee(https://apigee.com/console/instagram)
によるフォローではうまく動きました。
以下、apigeeの結果です。
Request
POST /v1/users/相手のユーザーID/relationship?access_token=取得したアクセストークン
HTTP/1.1
Host:api.instagram.com
Content-Length:13
X-Target-URI:https://api.instagram.com
Content-Type:text/plain; charset=UTF-8
Connection:Keep-Alive
Content-Language:en
X-Ratelimit-Limit:5000
Date:Mon, 06 Jul 2015 01:23:45 GMT
Vary:Cookie, Accept-Language
Content-Length:89
Expires:Sat, 01 Jan 2000 00:00:00 GMT
X-Ratelimit-Remaining:4997
Set-Cookie:クッキー; expires=Mon, 04-Jul-2016 01:23:45 GMT; Max-Age=31449600; Path=/
Connection:keep-alive
Content-Type:application/json; charset=utf-8
Cache-Control:private, no-cache, no-store, must-revalidate
Pragma:no-cache
{
"meta": {
"code": 200
},
"data": {
"outgoing_status": "follows",
"target_user_is_private": false
}
}
何か原因等お気づきの点がありましたら、コメントいただけるとありがたいです。
【2015.07.07追記01】
以下の申請箇所を見つけた為、入力・申請をかけてみました。
https://help.instagram.com/contact/185819881608116
【2015.07.07追記02】
ユーザーがアクセスした場合、一度instagramの認証画面に飛びます。
その際の表示内容ですが、他サイトでは
Hi 自分の名前 ,アプリ名 is requesting to do the following:
Access your basic information
includes photos, friend lists & profile info
Follow/unfollow other users
Only with your express permission
Like photos
Only with your express permission
と表示され、Authorizeボタンを押して認証する形ですが、自分の作成したWebアプリでは、
Hi 自分の名前 ,アプリ名 is requesting to do the following:
Access your basic information
includes photos, friend lists & profile info
としか表示がありませんでした。
当然、scope部分を確認しましたが、認証画面のURL自体も
https://instagram.como/auth/authorize/?client_id=クライアントID&redirect_uri=リダイレクトURI&scope=basic+comments+relationships+likes&response_type=code
となっており、relationshipsのパーミッション設定をしていたはずなのですが、認証画面に反映されていないようです。
引き続き、何か原因等お気づきの点がありましたら、コメントいただけるとありがたいです。
追記【2015.07.13】
Instagram Manage ClientsのEdit Client内、Enforce signed requestsへチェックを入れ、
https://instagram.com/developer/clients/manage/
Instagram デベロッパーページの
https://instagram.com/developer/secure-api-requests/
を参考に、
<?php
function generate_sig($endpoint, $params, $client_secret) {
$sig = $endpoint;
ksort($params);
foreach ($params as $key => $val) {
$sig .= "|$key=$val";
}
return hash_hmac('sha256', $sig, $client_secret, false);
}
$params = array(
'access_token' => 取得したアクセストークン,
"action" => "follow",
);
$client_secret = 取得したクライアントシークレット;
$sig = generate_sig(https://api.instagram.com/v1/users/相手のユーザーID/relationship, $params, $secret);
echo $sig;
?>
で署名を取得。
Windowsのコマンドプロンプト上で、
codeC:\>curl -k -X POST -F 'access_token=取得したアクセストークン' -F 'action=follow' -F 'sig=生成した署名' https://api.instagram.com/v1/users/相手のユーザーID/relationship
と入力したところ、
{"meta":{"error_type":"OAuthParameterException","code":400,"error_message":"Missing access_token URL parameter."}}
と出力されてしまいます。
引き続き、何か原因等お気づきの点がありましたら、コメントいただけるとありがたいです。