-
投稿
MBC1107 2019年8月6日
Companies(事業所)のGET をするPHPのプログラムがうまく行きません。どこがおかしいか教えてください。
マニュアルにあった。
curl -i -X GET \
-H “Authorization:Bearer {access_token}” \
-H “Content-Type:application/json” \
‘https://api.freee.co.jp/api/1/companies’をPHPで実行したいと思い、書いてみました。
上記のcurl を手動で実行すると問題なく動きますが、添付のソースでは、返り値が正しく出てきません。
401 Unauthorized となり、{“message”:”ログインをして下さい”} が返ってきます。
どこが悪いのか教えてください。
PHPのプログラムは以下の通りです。
<?php
print( “————[company_access.php]————
” );
$access_token = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
print(“アクセストークン=[“.$access_token.”]
” );//———– Companies(事業所)のGET ——————
/*
curl -i -X GET \
-H “Authorization:Bearer {access_token}” \
-H “Content-Type:application/json” \
‘https://api.freee.co.jp/api/1/companies’
*/
$headers = array( ‘Authorization’ => ‘Brearer ‘.$access_token ,
‘Content-type’ => ‘application/json’
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ‘https://api.freee.co.jp/api/1/companies’);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, ‘GET’);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, true);$response = curl_exec($curl);
print( “<textarea rows=50 cols=150>”.$response.”</textarea><br>” );
?>
- 回答するにはログインが必要です。
回答
nick 2019年8月6日
こんにちは。
phpのソースコードを拝見したところ
‘Authorization’ => ‘Brearer ‘.
となっていますが、これは
‘Authorization’ => ‘Bearer ‘.
が正しいのではないでしょうか。
ちょっと気になり返信しました。
ご確認ください。
MBC1107 2019年8月7日
ありがとうございます。「あ~!タイプミス!」
と思って、Bearer にしてみましたが、結果が一緒でした。
ほかにも、問題がありそうです。教えてください。
念のために、今度のソースを下記に書きます。
<?php
print( “————[company_access.php]————<br>” );
$access_token = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
print(“アクセストークン=[“.$access_token.”]<br>” );//———– Companies(事業所)のGET ——————
$headers = array( ‘Authorization’ => ‘Bearer ‘.$access_token ,
‘Content-type’ => ‘application/json’
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ‘https://api.freee.co.jp/api/1/companies’);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, ‘GET’);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, true);$response = curl_exec($curl);
print( “<textarea rows=50 cols=150>”.$response.”</textarea><br>” );
?>textarea で表示されたコードは
—————————————————————
HTTP/1.1 401 Unauthorized
Date: Wed, 07 Aug 2019 00:32:35 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 401 Unauthorized
Cache-Control: no-cache
X-XSS-Protection: 1; mode=block
X-Request-Id: xxxxxxxxxxxxxxxxxxxxx
X-Runtime: 0.011319
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff{“message”:”ログインをして下さい”}
—————————————————————-
です。
kkkokumura 2019年8月7日
アクセストークンの有効期限切れ、もしくは入力間違いということはありませんか?
私はそれで解決しました。nick 2019年8月8日
こんにちは。
気になって、もう少しコードを見直したところ、headerの指定を修正することでうまく動きました。
$headers = array( ‘Authorization’ => ‘Brearer ‘.$access_token ,
‘Content-type’ => ‘application/json’を
$headers = array( ‘Authorization:Brearer ‘.$access_token ,
‘Content-type:application/json’に修正。
https://www.php.net/manual/ja/function.curl-setopt.php
お試しください!
MBC1107 2019年8月9日
$headers = array( ‘Authorization:Bearer ‘.$access_token ,’Content-type:application/json’ );
とすることでうまく行きました。ありがとうございます。
ちょっと不思議なのは、アクセストークンを取得する PHP では、
$headers = array( ‘Content-type:application/json’ );
ではうまくいかず、
$headers = array( ‘Content-type’ => ‘application/json’ );
でうまくいきました。
なんででしょうね~・・・・
freee dev-support 2019年8月14日
お世話になっております。
無事取得できるようになり、何よりでございます。
nick様もご回答していただき、誠にありがとうございます。
また他にご不明な点がございましたら、お気軽にお申し付けくださいませ。
どうぞ宜しくお願い致します。
Community
コミュニティをご利用の際は 【ガイドライン】を確認・同意いただいた上でご利用ください。