首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于在php中使用Twitter-Oauth API的问题

关于在php中使用Twitter-Oauth API的问题
EN

Stack Overflow用户
提问于 2011-01-21 06:11:10
回答 3查看 1.6K关注 0票数 6

最近,我读了一个不错的教程如何使用Twitter OAuth认证用户,即使它是在更改twitter格式之前编写的,但是它也适用于新的Twitter格式。

我有一些问题,请解释一下,如果有人成功的话。

  • 为什么我们总是调用两个方法getRequestTokengetAccessToken?是为了得到access tokenaccess token secret吗?但这两种情况都已经在下面的页.http://dev.twitter.com/apps/{your_app_id}/my_token上给出了。 请求令牌请求令牌秘密究竟需要什么?虽然我注意到,每次处理时,这两个令牌都不同。
  • 如果我们从下面的方法中更新我们的状态 $connection->post( 'statuses/update', array('status' => 'some message got from text area value' );

那么如何验证状态是否已成功更新??这意味着如果我想显示警告消息post has been sent successfully,我如何在我们的PHP页面实现它?

  • 哪个回调URL是重要的,也就是说,在twitter上发布或做一些事情之后,实际用户在哪里导航?
代码语言:javascript
复制
1. is it the URL `Registered OAuth Callback URL` which is written at the time of developing an application on 

http://dev.twitter.com/apps/{id_no}

2.在我们的php代码(config.php)中定义的URL是否类似

define('OAUTH_CALLBACK', 'http://www.xyz.com');

再来一个Q'n

泰语更新

我按照你的建议做了下面的事

代码语言:javascript
复制
$user_info = $connection->get('account/verify_credentials');
$status_info =$connection->get('statuses/show/', array('id' =>32320907720523776) );

echo "<pre>";
print_r($status_info);

echo "</pre> Content : <pre>";
print_r($user_info);

以下回报

代码语言:javascript
复制
stdClass Object
(
    [request] => /1/statuses/show.json?id=3.2320907720524E%2B16&oauth_consumer_key=jashak..&oauth_nonce=bec...&oauth_signature=%2FMj%2B0Z7oyYNKdMn%2B%2FOJ6Ba8ccfo%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1296541384&oauth_token=223961574-cW4...&oauth_version=1.0a
    [error] => No status found with that ID.
)

注意:为了安全起见,我隐藏了oauth_consumer keyoauth_nonceoauth_token ;)

代码语言:javascript
复制
Content:
stdClass Object
(
    [follow_request_sent] => 
    [profile_link_color] => 038543
    [profile_image_url] => http://a3.twimg.com/profile_images/1215444646/minialist-photography-9_normal.jpg
    [contributors_enabled] => 
    [favourites_count] => 31
    [profile_sidebar_border_color] => EEEEEE
    [id_str] => 223961574 // this is also id_str
    [status] => stdClass Object
        (
            [retweeted] => 
            [id_str] => 32320907720523776 // this id_str i used
            [in_reply_to_status_id_str] => 
            [geo] => 
            [contributors] => 
            [source] => Black Noise
            [in_reply_to_user_id_str] => 
            [retweet_count] => 0
            [truncated] => 
            [coordinates] => 
            [created_at] => Tue Feb 01 06:14:39 +0000 2011
            [favorited] => 
            [text] => Twitter test: verify that status has been updated
            [place] => 
            [in_reply_to_screen_name] => 
            [in_reply_to_status_id] => 
            [id] => 3.2320907720524E+16
            [in_reply_to_user_id] => 
        )
   [screen_name] => ltweetl
   [profile_use_background_image] => 1
   ....
   ...

我搞错了No status found with that ID,你提到的是哪个id_str??

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-31 13:31:18

这个答案并不是专门针对亚伯拉罕的Twitter,而是适用于Twitter的OAuth API。

  1. 该页面只为您提供您的访问令牌和您自己的应用程序的访问令牌秘密。如果您不需要您的应用程序作为任何其他用户进行身份验证,那么就不需要请求请求令牌,也不需要将请求令牌交换为访问令牌,只需使用您的访问令牌即可。 但是,如果您想作为其他用户进行身份验证,则必须完成所有所需的步骤,以下将对此进行简要说明:
代码语言:javascript
复制
- You request the **request** token. It is only used for signing in, and cannot be used to access user's data. You will get the request token and request token secret. When requesting the request token, **you can specify a callback URL** which Twitter will send your users to when it is authenticated successfully.
- You need to keep the request token and secret until the authentication is done.
- After that, you redirect the user to `http://api.twitter.com/oauth/authorize?oauth_token=` followed by OAuth token.
- After the user signed in to Twitter and allowed your application, Twitter sends the user back to the callback URL.
- You exchange the request token for the access token, and you can then discard the request token because you won't need it anymore.

只要您需要访问令牌,就可以保留它。如果不保留访问令牌,则需要再次请求请求令牌并将其交换为访问令牌,用户将不得不再次登录。

因此,基本上,如果您正在创建用户需要使用Twitter登录的内容,您需要执行上述所有步骤才能让用户注册。如果您只是为自己使用Twitter的API,则不需要身份验证步骤。使用您的访问令牌.

  1. 您可以通过检查返回的响应上的键来检查tweet的ID。 如果状态未发布,Twitter将返回一个错误对象。
  2. 您可以在“应用程序设置”页面中指定默认 OAuth回调,当您没有显式指定回调时将使用该回调。 这是必需的,因为如果您忘记或没有指定回调URL,Twitter仍将知道将用户重定向到何处。 但是,Twitter鼓励您应该显式地指定回调URL。使用回调URL有许多好处,例如能够指定任何URL作为回调。我曾经从中受益,因为我的Twitter客户端运行在两个不同的域上,我可以将用户重定向回正确的位置。
票数 4
EN

Stack Overflow用户

发布于 2011-01-21 06:15:58

因为只有在我们用验证器验证用户之后才能获得AccessToken和访问令牌秘密。

验证器在OAuth process.So中是最重要的,我们需要发送一个请求来生成AccessToken和访问令牌秘密。

在获得AccessToken和访问令牌秘密之后,我们可以在用户指定的时间内使用它,同时允许您的Twitter应用程序允许访问他的Info.it可能是1天,就像LInkedin OAuth中的wise一样,此后AccessToken和Access令牌秘密到期.

票数 0
EN

Stack Overflow用户

发布于 2011-01-31 12:39:59

当你做的时候你会得到什么:

代码语言:javascript
复制
$temp = $connection->post( 'statuses/update', array('status' => 'some message got from textarea value' );
print_r($temp);

我猜这会将响应(以json/xml)格式存储在$temp var中。(要测试twitter响应,您可以检查http://dev.twitter.com/console)

在config.php中定义的回调url将确定它将被重定向到何处。这是因为这个回调参数最近被添加到twitter中。在此之前,您只能在twitter.com的App部分定义回调url,如果没有定义其他URL,则此url是默认的url。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4755933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档