1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TwitterAPI で、OAuth認証の署名を作成する際に使用するURLに ? を付けるとエラーになる

Last updated at Posted at 2019-04-30

エラーの内容

code : 32
message : Could not authenticate you.

エラーの原因,解決策

とても単純で、OAuth認証の署名を作成する際に使用するURLの末尾に ? が付いていることが原因です。
クエリを付ける場合は、署名を作成した後にURLの末尾に ? を付けましょう。

環境

PHP
Twitter API

上記のようなエラーが発生してしまうケース

下記のコードは署名部分の詳細なコードは省いてます。

example_failure.php
// APIのリクエスト先URL
// ? を付けてしまうとエラーとなってしまいます
$request_url = 'https://api.twitter.com/1.1/users/show.json?' ;

/* OAuthの署名作成処理部分 */

// クエリとしてAPIへ送信するパラメータ
$params_a = array(
	"screen_name" => $twitter_id,
	"include_entities" => "false",
) ;

// パラメータがある場合、URLの末尾に追加
if( $params_a ) {
	$request_url .= http_build_query( $params_a ) ;
}

動作する例を以下に示します。

example_success.php
// APIのリクエスト先URL
// ? はここで付けない
$request_url = 'https://api.twitter.com/1.1/users/show.json' ;

/* OAuthの署名作成処理部分 */

// クエリとしてAPIへ送信するパラメータ
$params_a = array(
	"screen_name" => $twitter_id,
	"include_entities" => "false",
) ;

// パラメータがある場合、URLの末尾に追加
if( $params_a ) {
//	ここで ? を付ける
	$request_url .= '?' . http_build_query( $params_a ) ;
}

とても単純なミスですが、エラーメッセージから原因を推測することが難しいですね・・・

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?