douzhang1852 2018-10-26 06:24
浏览 456
已采纳

使用承载令牌在Twitter API中获取用户信息

I am using twitter API in my Laravel application. In my app I want to get user name and profile picture when user insert url by. I searched lot but couldn't found. For user info I had to make user login to twitter and connect with my app for access token. I want to use App authorization only so user don't need to login twitter. I found that with Bearer token tweets can be search with this search API

https://api.twitter.com/1.1/search/tweets.json?q=twitterapi

Is there any API's that I can get User screen name using URL?

I know there may be posts that are similar with this, most answers with usl says "This page doesn't exists" or saying to use Oauth.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongluan5740 2018-12-14 08:57
    关注

    Ah, I was so lazy to read whole documentation.
    Here is everything about App-only authentication. All you need to do is firstly encode consumer key and secret in RFC 1738 and then re-encode them in

    Base64 "RFC-1738-encoded-consumer-key:RFC-1738-encoded-consumer-secret"
    

    format. You will get bearer token credentials like

    eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw== this.

    Now you all you need to do is get bearer token with post request with

    https://api.twitter.com/oauth2/token?grant_type=client_credentials this URL.

    Be sure to set header with

    Authorization: Basic bearer-token-credentials
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 29 
    

    and you will get Bearer Token in json format.

    Now with every request, just send this bearer token in header like

    Authorization: Bearer bearer-token
    

    and you can get data which doesn't require user authentication. For example, if you want to get User data by screen name or id, just send post request in https://api.twitter.com/1.1/users/show.json?screen_name=here-comes-twitter-screen-name URL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划