使用twitter api获取令牌 [英] using twitter api to get token

查看:498
本文介绍了使用twitter api获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用twitter api,但需要进行身份验证.有2种类型,我只需要Application-only authentication aka app only.这是一种身份验证,其中应用程序代表自己发出API请求.

I am trying to use the twitter api, but need to get authentication. There are 2 types , and I only need Application-only authentication aka app only. This is the type of authentication where an application makes API requests on its own behalf.

文档说明要使用此方法,您需要使用不记名令牌.您可以通过在POST oauth2/令牌端点传递您的使用者密钥和机密来生成承载令牌.

The docs explain to use this method, you need to use a bearer token. You can generate a bearer token by passing your consumer key and secret through the POST oauth2 / token endpoint.

这里是链接到解释此端点的文档的链接.甚至有一个示例请求,但对我来说仍然不清楚要做什么.

Here is the link to docs explaining this endpoint. There is even an example request but still it isn't very clear to me what needs to be done.

我有一个API密钥和API秘密密钥,但是出现以下错误:

I have an API key and API secret key, but am getting the following error:

正文:‘{错误":[{代码":170,消息":缺少必需的参数: grant_type","label":"forbidden_​​missing_parameter"}]}’}

body: ‘{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}’ }

我的服务器端代码如下

var request = require('request');
var btoa = require('btoa');

const KEY = encodeURIComponent('1234');
const SECRET = encodeURIComponent('5678');

request({
    headers: {
      'Authorization': 'Basic ' + btoa(`${KEY}:${SECRET}`),
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    uri: 'https://api.twitter.com/oauth2/token',
    method: 'POST',
    body:  JSON.stringify({
      'grant_type': 'client_credentials' // I am passing the grant_type here
    })
  }, function (err, res, body) {
    console.log('res', res)
  });

文档中的CURL请求如下所示:

The CURL request in the docs looks like the following:

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2R ... o4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

推荐答案

为此,有两件事.首先,需要在服务器端发出请求.您需要从npm安装btoa来提供密钥和秘密密钥的编码. KEY和SECRET需要用冒号分隔.请求的正文必须为字符串

To to this there were a couple of things. First the request needed to be made server side. You need to install btoa from npm to provide the encoding of the key and secret key. The KEY and SECRET need to be separated by a colon. The body of the request needs to be a string of

'grant_type=client_credentials'

请参阅下面的完整代码示例.

See full code example below.

const btoa = require('btoa');

request({
    headers: {
      'Authorization': 'Basic ' + btoa(`${KEY}:${SECRET}`),
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    uri: 'https://api.twitter.com/oauth2/token',
    method: 'POST',
    body: 'grant_type=client_credentials'
  }, (error, response, body) => {
    const token = JSON.parse(body).access_token;
  });

这篇关于使用twitter api获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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