开始使用Twitter开发人员平台

推特授权使用的还是oauth1.0的模式,官网介绍比较麻烦,也很不友好,所以下面将介绍一种简单的授权方法。

from __future__ import print_function

from requests_oauthlib import OAuth1Session


REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'

consumer_key = '********'
consumer_secret = '*********'


def get_auth_token():
    oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
                                 callback_uri='oob')

    resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL, )
    url = oauth_client.authorization_url(AUTHORIZATION_URL, auth_callback='oob')
    data = {
        "url": url,
        "oauth_token_secret": resp.get('oauth_token_secret')
    }
    return data


def get_access_token(oauth_token, oauth_token_secret, pincode):
    oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
                                 resource_owner_key=oauth_token,
                                 resource_owner_secret=oauth_token_secret,
                                 verifier=pincode)
    # try:
    #     resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
    # except ValueError as e:
    #     raise 'Invalid response from Twitter requesting temp token: {0}'.format(e)

    resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)

    print('''Your tokens/keys are as follows:
        consumer_key         = {ck}
        consumer_secret      = {cs}
        access_token_key     = {atk}
        access_token_secret  = {ats}'''.format(
        ck=consumer_key,
        cs=consumer_secret,
        atk=resp.get('oauth_token'),
        ats=resp.get('oauth_token_secret')))

print(get_auth_token())  #1
get_access_token("111111111111111","22222222222",'333333333')  #2

1.先把consumer_keyconsumer_secret换成自己的
2.注释get_access_token("111111111111111","22222222222",'333333333'),运行代码,结果如下:
{'url': 'https://api.twitter.com/oauth/authorize?auth_callback=oob&oauth_token=111111111111111', 'oauth_token_secret': '22222222222'}
找个地方记下oauth_tokenoauth_token_secret参数的值

https://api.twitter.com/oauth/authorize?auth_callback=oob&oauth_token=111111111111111这个url复制到浏览器中,如果浏览器已经有登录好的推特账号,那么就会出现一个授权界面

推特开发者 推特 oauth1.0a 授权
授权页面

点击授权应用程序,然后就会出现一个pincode

推特开发者 推特 oauth1.0a 授权
pincode

记住这个pincode。

3.把print(get_auth_token())注释。
get_access_token("111111111111111","22222222222",'333333333')的注释取消,把oauth_tokenoauth_token_secretpincode作为参数写进去,运行。
结果如下:

推特开发者 推特 oauth1.0a 授权
key

这样就完成了授权。

4.在postman中把刚才的参数写进去,postman如何使用twitter api可以在官网看到:https://documenter.getpostman.com/view/9956214/T1LMiT5U

推特开发者 推特 oauth1.0a 授权

在postman中构造请求,ids的参数是授权用户的某个推文id:
https://api.twitter.com/2/tweets?tweet.fields=non_public_metrics&ids=***********&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text,non_public_metrics,promoted_metrics,organic_metrics

getdata
返回结果

可以看到返回中出现了授权用户的推文访问量。

相关产品

关注微信
关注微信
分享本页
返回顶部