Session, PHP Incomplete Class

前端 未结 2 1617
夕颜
夕颜 2020-12-21 09:46

I am using cakePHP 2.x . Currently doing about the twitter OAuth, http://code.42dh.com/oauth/.

function twitter_authentication()
{
            //assume above         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 10:07

    OAuth.php's OauthToken class is quite simple with just two properties: key and secret. When you get the login url, you can store it to the session as an array:

    CakeSession::write('Twitter.requestToken', array(
        'key' => $requestToken->key,
        'secret' => $requestToken->secret
    ));
    

    Then, instantiate your own OAuthToken when calling OAuthClient->getAccessToken() like so:

    $sessionRequestToken = CakeSession::read('Twitter.requestToken');
    $accessToken = $twitterClient->getAccessToken('https://api.twitter.com/oauth/access_token', 
        new OAuthToken($sessionRequestToken['key'], $sessionRequestToken['secret']));
    

    Should be ready to go:

    if ($accessToken) {
        $twitterClient->post($accessToken->key, $accessToken->secret, 
            'https://api.twitter.com/1/statuses/update.json', array('status' => 'My balls smells like A-1 sauce. #science'));
    }
    

提交回复
热议问题