Tumblr.js 401 未经授权

Tumblr.js 401 Unauthorized

这有什么问题?

var tumblr = require('tumblr.js');
var client = tumblr.createClient({
  consumer_key: '...',
  consumer_secret: '...',
  token: '...',
  token_secret: '...'
});

client.userInfo(function (err, data) {
    if (err)
        console.log(err);
    console.log(JSON.stringify(data,null,'\t'));
});

每个密钥都在其位置,并且无论如何都在 Chrome 控制台“401 未授权”中! 我正在本地主机上使用 'simple-react-app' 和官方 'tumblr.js' 模块。它可以是什么?顺便说一句,博客的方法工作正常但用户 - 信息,破折号,都失败了。

401 未经授权

If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.

参见:W3/protocols

在浏览器中

Due to CORS restrictions, you're going to have a really hard time using this library in the browser. Although GET endpoints on the Tumblr API support JSONP, this library is not intended for in-browser use. Sorry!

https://github.com/tumblr/tumblr.js/#in-the-browser

跨源资源共享 (CORS)

The Cross-Origin Resource Sharing (CORS) mechanism gives web servers cross-domain access controls, which enable secure cross-domain data transfers. Modern browsers use CORS in an API container - such as XMLHttpRequest or Fetch - to mitigate risks of cross-origin HTTP requests.

参见:MDN

Tumblr 控制台

在此处尝试您的代码,并检查您的 api 密钥和令牌(如果它们有误,可能会导致 401 错误)

https://api.tumblr.com/console/calls/user/dashboard

Tumblr Api

您可以使用普通的 api(这只是 node.js 的包装器):

只需执行一个 GET 请求:

api.tumblr.com/v2/user/info

参见:API

超级特工

SuperAgent is a small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features.

request
  .get('http://api.example.com')
  .auth('user', 'pass123')
  .end(callback);

参见:Docs

例子

Note: this is just a quick and dirty implementation of tumblr.js and react, please don't use this code directly and instead make your own implementation.

顺便说一句:欢迎 PR!

github/btzr-io/react-tumblr

设置:安装依赖和运行npm start启动服务器

 npm install & npm start

MasterKey 替换为您在 dist/server.js 中的实际令牌/密钥:

// Authenticate via OAuth
const tumblr = require('tumblr.js');
const client = tumblr.createClient(MasterKey);