twitter api

console

https://developer.twitter.com

和google FB不一样,即使是简单的登录的开发者权限也依然是需要批准的
这篇是2019年6月写的,使用中国手机申请会被拒绝,不过可以用google voice注册
google voice 同样不支持中国手机号绑定

oauth

twitter 没有提供封装好的前端sdk,只有
https://developer.twitter.com/en/docs/developer-utilities/twitter-libraries.html

oauth 文档:
https://developer.twitter.com/en/docs/twitter-for-websites/log-in-with-twitter/guides/implementing-sign-in-with-twitter
Authorization 头生成:
https://developer.twitter.com/en/docs/basics/authentication/guides/authorizing-a-request
Authorization 头 中的signature 生成:
https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html

twitter 的oauth是最麻烦的,可能因为是oauth1.0
所有oauth 有关的api,可以看到其中oauth2 只有两个api
https://developer.twitter.com/en/docs/basics/authentication/api-reference/invalidate_bearer_token
从下面对oauth的说明中,我们可以知道,
一些只读且和用户无关的操作可以用个oauth2 的token( bearer token) 调用,
另外一些比如发推这种操作就需要Application-user authentication( OAuth 1a access token for user context)
https://developer.twitter.com/en/docs/basics/authentication/overview/oauth

oauth2 的文档和例子:
https://developer.twitter.com/en/docs/basics/authentication/api-reference/token
https://developer.twitter.com/en/docs/basics/authentication/guides/bearer-tokens.html

Access token & access token secret 生成和作用(应该就是只能访问自己的资源的相当于从oath1a的最后一步开始)
https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens.html
https://developer.twitter.com/en/docs/basics/authentication/guides/single-user

用户信息

文档:
https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials

注意这里是 这个api 是 user context only 的,也就是只支持 oauth1

关于安全

这个似乎可以玩一下:
https://www.owasp.org/index.php/Category:OWASP_WebGoat_Project
https://www.owasp.org/index.php/Main_Page

### 使用 Twitter API 进行开发 #### 创建开发者账号并获取API密钥 为了使用Twitter API,需要先注册成为Twitter开发者,并创建应用程序来获得必要的认证凭证。这包括消费者密钥(Consumer Key)、消费者秘密(Consumer Secret)、访问令牌(Access Token)以及访问令牌秘密(Access Token Secret)[^2]。 #### 安装库文件 对于Python开发者来说,推荐安装`Tweepy`这个第三方库,它简化了许多与Twitter API交互的过程。可以通过pip命令轻松安装此库: ```bash pip install tweepy ``` 而对于Node.js环境下的开发者,则可以选择`node-twitter-api-v2`作为接口调用工具[^3]。同样地,也可以利用npm来进行包管理器安装操作: ```bash npm install twitter-api-v2 ``` #### 编写代码连接至API 一旦获得了所需的认证信息并且选择了合适的客户端库之后,就可以编写简单的程序来测试是否能成功接入Twitter API了。下面给出一段基于Python (Tweepy) 的例子,展示怎样验证身份并向控制台打印出当前登录用户的名称[^1]: ```python import tweepy consumer_key = "YOUR_CONSUMER_KEY" consumer_secret = "YOUR_CONSUMER_SECRET" access_token = "YOUR_ACCESS_TOKEN" access_token_secret = "YOUR_ACCESS_TOKEN_SECRET" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) try: user = api.verify_credentials() print(f"Connection successful! User name is {user.name}") except Exception as e: print("Error during connection:", str(e)) ``` 同样的逻辑,在JavaScript(Node.js) 中实现如下所示: ```javascript const { TwitterApi } = require('twitter-api-v2'); (async () => { const client = new TwitterApi({ appKey: 'YOUR_CONSUMER_KEY', appSecret: 'YOUR_CONSUMER_SECRET', accessToken: 'YOUR_ACCESS_TOKEN', accessSecret: 'YOUR_ACCESS_TOKEN_SECRET' }); try { const me = await client.v2.me(); console.log(`Connected successfully! Username is ${me.data.username}`); } catch (error) { console.error('Failed to connect:', error); } })(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值