通过JavaScript访问Twitter [英] Accessing Twitter through Javascript

查看:142
本文介绍了通过JavaScript访问Twitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个web应用程序,使用微博的方式如下:

1)A的用户可以登录与Twitter(即用户认证和授权的应用程序)-I得到下面的一段: oauth_token_secret = [OAUTH_TOKEN_SECRET_HERE]放大器; oauth_token = [OAUTH_TOKEN_HERE]这是我保存到服务器

2)现在,我想纯粹访问只使用的JavaScript客户端(使用jQuery)的用户数据。那么,如何做呢???我试了几个办法,我一直缺少的东西,所以我的问题是如何调用进行?都需要在例如什么参数:

<$p$p><$c$c>$.getJSON('https://api.twitter.com/1/statuses/home_timeline.json?include_entities=true&[WHAT参数SHOLD加入HERE ???]放大器;回调=',功能(数据){     的console.log(数据);     });

解决方案

您可以使用计数 AMD max_id 查询参数,但你并不需要它们。

<打击>我也不认为你需要或想要的回调=?部分。

最关键的事情:你需要使用这些OAUTH项目,从而产生Authorization头,你必须添加到出站请求。头的结构 Twitter的开发者文档

要看到这些消息是什么样子,请访问: https://apigee.com/console/twitter

Thsi是一个有效的请求的例子:

  GET /1/statuses/home_timeline.json HTTP / 1.1
授权:XXXXXXXXX
主持人:api.twitter.com
用户代理:Mozilla的/ 5.0(.....)
 

...其中XXXXXX是Authorization头,按规定由Twitter的OAuth的1.0a的规则生成。

它看起来是这样的:(换行插入可读性,这个头应该是所有在实际的请求一行)

  OAuth的oauth_consumer_key =RR0sePOBbQ8bYdC8r41mg
  oauth_signature_method =HMAC-SHA1,
  oauth_timestamp =1341705465,
  oauth_nonce =34350,
  oauth_version =1.0,
  oauth_token =5915213-haTzPfWcr6Ci2gdnD8797AfgRs8AICDK8KIStFtx
  oauth_signature =1hxDP104ZXGMlcblQ05h6vWoJg%3D
 

您可能想利用一个Javascript的OAuth库,以构建一个头,并发送请求。

尝试搜索jsOAuth和Twitter

I'm building a web app that uses twitter in the following way:

1) A users can sign in with Twitter (i.e. a user authenticates and authorizes the app) -I get the following piece: "oauth_token_secret=[OAUTH_TOKEN_SECRET_HERE]&oauth_token=[OAUTH_TOKEN_HERE]" which I store to the server.

2) Now I want to access the users data purely on the client side using only javascript (with jQuery). So how do I do it??? I've tried a couple of approaches and I keep missing something, so my question is how should the calls be made? What parameters are needed in the example:

$.getJSON('https://api.twitter.com/1/statuses/home_timeline.json?include_entities=true&[WHAT PARAMETERS SHOLD BE ADDED HERE???]&callback=?', function(data) {
    console.log(data);
    });

解决方案

You can use count amd max_id query parameters, but you don't need them.

I also don't think you need or want the callback=? part.

The key thing: You need to use those OAUTH items to produce an Authorization header, which you must add to the outbound request. The structure of the header is described in Twitter's developer documentation.

To see what the messages look like, go to: https://apigee.com/console/twitter

Thsi is an example of a valid request:

GET /1/statuses/home_timeline.json HTTP/1.1
Authorization: XXXXXXXXX
Host: api.twitter.com
User-Agent: Mozilla/5.0 (.....)

...where the XXXXXX is the Authorization header, generated according to the oauth 1.0a rules stipulated by Twitter.

It looks like this: (line breaks inserted for readability; this header should be all on one line in the actual request)

OAuth oauth_consumer_key="RR0sePOBbQ8bYdC8r41mg",
  oauth_signature_method="HMAC-SHA1",
  oauth_timestamp="1341705465",
  oauth_nonce="34350",
  oauth_version="1.0",
  oauth_token="5915213-haTzPfWcr6Ci2gdnD8797AfgRs8AICDK8KIStFtx",
  oauth_signature="1hxDP104ZXGMlcblQ05h6vWoJg%3D"

You might want to take advantage of a Javascript OAuth library in order to construct that header, and send the request.

Try searching jsOAuth and Twitter

这篇关于通过JavaScript访问Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆