imsamurai/http-socket-oauth

用于认证授权的HTTP套接字

1.0.1 2014-02-10 16:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:03:18 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

使用说明(以twitter为例)

  1. 从我的github账户获取代码,并将其添加到app/vendors/http_socket_oauth.php

  2. 在Twitter上注册您的应用程序(如果您在本地上开发且Twitter对包含'localhost'的回调URL表示不满,请参考下文)

  3. 注意消费者密钥和密钥(我将它们添加到bootstrap中的配置中)

  4. 将以下内容添加到控制器中

     public function twitter_connect() {
       // Get a request token from twitter
       App::import('Vendor', 'HttpSocketOauth');
       $Http = new HttpSocketOauth();
       $request = array(
         'uri' => array(
           'host' => 'api.twitter.com',
           'path' => '/oauth/request_token',
         ),
         'method' => 'GET',
         'auth' => array(
           'method' => 'OAuth',
           'oauth_callback' => '<enter your callback url here>',
           'oauth_consumer_key' => Configure::read('Twitter.consumer_key'),
           'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'),
         ),
       );
       $response = $Http->request($request);
       // Redirect user to twitter to authorize  my application
       parse_str($response, $response);
       $this->redirect('http://api.twitter.com/oauth/authorize?oauth_token=' . $response['oauth_token']);
     }
    

    ... 替换为您的回调URL,即Twitter将用户重定向回的页面URL。在本例中,它是下一步操作的URL。注意,当您注册应用程序时,如果您在本地上开发,并且尝试将包含localhost的回调URL输入其中,Twitter可能会表示不满。我在某处读到的一个小宝石说您实际上可以创建一个bit.ly链接,在其中添加您本地的回调URL,然后将bit.ly链接添加到Twitter应用程序设置中的回调URL。尽管如此,我还是在这里添加了localhost URL)。

    此操作从Twitter获取请求令牌,并将其作为查询字符串参数添加到Twitter.com上的授权URL,用户将被重定向到该URL,并提示他们授权您的应用程序访问他们的帐户。

  5. 然后添加回调操作

     public function twitter_callback() {
       App::import('Vendor', 'HttpSocketOauth');
       $Http = new HttpSocketOauth();
       // Issue request for access token
       $request = array(
         'uri' => array(
           'host' => 'api.twitter.com',
           'path' => '/oauth/access_token',
         ),
         'method' => 'POST',
         'auth' => array(
           'method' => 'OAuth',
           'oauth_consumer_key' => Configure::read('Twitter.consumer_key'),
           'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'),
           'oauth_token' => $this->params['url']['oauth_token'],
           'oauth_verifier' => $this->params['url']['oauth_verifier'],
         ),
       );
       $response = $Http->request($request);
       parse_str($response, $response);
       // Save data in $response to database or session as it contains the access token and access token secret that you'll need later to interact with the twitter API
       $this->Session->write('Twitter', $response);
     }
    

    用户授权您的应用程序后,Twitter将他们重定向回此操作,即您在之前的请求中指定的回调。查询字符串中有两个参数,分别称为'oauth_token'和'oauth_verifier'。然后,将这些参数以及消费者密钥和密钥发送回Twitter,这次请求访问令牌。

    在此操作的末尾,$response包含一个关联数组,其中包含以下键:'oauth_token'、'oauth_token_secret'、'user_id'、'screen_name'。您应该将'oauth_token'和'oauth_token_secret'保存到会话或数据库中,以便在您需要访问Twitter API时使用它们。然后,将用户重定向到另一个操作,或显示感谢信息或推文到他们的帐户,或做任何其他事情。

现在,如果您链接到twitter_connect()操作或在其浏览器地址栏中输入它,您应该被重定向到Twitter以授权您的应用程序,一旦完成,就会回到您的应用程序中,并带有一些Twitter帐户访问令牌。

最后,我想知道如何使用这种新获得的力量做些与Twitter API相关的事情

      App::import('Vendor', 'HttpSocketOauth');
      $Http = new HttpSocketOauth();
      // Tweet "Hello world!" to the twitter account we connected earlier
      $request = array(
        'method' => 'POST',
        'uri' => array(
          'host' => 'api.twitter.com',
          'path' => '1/statuses/update.json',
        ),
        'auth' => array(
          'method' => 'OAuth',
          'oauth_token' => $oauthToken, // From the $response['oauth_token'] above
          'oauth_token_secret' => $oauthTokenSecret, // From the $response['oauth_token_secret'] above
          'oauth_consumer_key' => Configure::read('Twitter.consumer_key'),
          'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'),
        ),
        'body' => array(
          'status' => 'Hello world!',
        ),
      );
      $response = $Http->request($request);

希望您喜欢它。如有任何问题,请留下github问题跟踪器。如有任何评论,请通过我的博客通知我。谢谢。