LoginSignup
20
18

More than 5 years have passed since last update.

twitter apiでつぶやけなかった時の解決策メモ

Posted at

twitterOauthでツイートする際にくだらない事で嵌ったのでメモ。

現象

twitterで認証して戻ってくるcallbackurlでは、statuses/updateを使ってツイートできる。
しかし、oauth_tokenとoauth_token_secretをCookieに保存して、別ページでstatuses/updateをすると、Invalid or expired token [code] => 89 がでて投稿できない。

原因

Callbackurlで使えたoauth_tokenとoauth_token_secretは別ページに行くと使えなくなる。
$access_token内にあるtokenを保存しなければならない。

コード

ポイント部分だけ。

qiita.php

$tw = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,
$_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

//アクセストークン所得
$access_token = $tw->getAccessToken($_REQUEST['oauth_verifier']);

//oauth_tokenとoauth_token_secret所得
$oauth_token=$access_token['oauth_token'];
$oauth_token_secret=$access_token['oauth_token_secret'];

//保存
setcookie("oauth_token",$oauth_token,time()+3600,'/');
setcookie("oauth_token_secret",$oauth_token_secret,time()+3600,'/');

$message = 'test';

//投稿
$tw->post('statuses/update', array('status' => $message));
20
18
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
20
18