本文介绍了向Chrome扩展程序内的Tumblr API发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用他们的API和chrome_ex_oauth向Tumblr发文.

I'm trying to make a text post to Tumblr using their API and chrome_ex_oauth.

  • API: http://www.tumblr.com/docs/en/api/v2#posting
  • chrome_ex_oauth: http://code.google.com/chrome/extensions/tut_oauth.html

获得授权作品的全过程.我无法开始的工作是进行POST.我正在执行以下操作:

The whole process of getting authorized works. What I can't get to work is doing a POST. I'm doing the following:

我已经更新了代码,以反映Rob W关于车身领域的正确建议

var stringify = function (parameters) {
  var params = [];
  for(var p in parameters) {
    params.push(encodeURIComponent(p) + '=' +
                encodeURIComponent(parameters[p]));
  }
  return params.join('&');
};

var onAuthorized = function() {
  var url = 'http://api.tumblr.com/v2/blog/jindie.tumblr.com/post';
  var request = {
    'method': 'POST',
    'headers':{
      'Content-Type':'application/x-www-form-urlencoded'
    },
    'body': stringify({
      'type': 'text',
      'state': 'draft',
      'title': 'Test post...',
      'body': 'Hello, World!'
    })
  };

  oauth.sendSignedRequest(url, function(responseText, xhr){alert(responseText);}, request);
};

oauth.authorize(onAuthorized);

我一直在检查代码,并思考可能出什么问题,但是我真的不知道.你呢?

I've been examining the code, and thinking what could be wrong, but I seriously have no idea. Do you?

你知道我要去哪里了吗?

Do you know where I'm going wrong?

推荐答案

当文档无助于查看源代码时, chrome_ex_oauth.js .

When the documentation doesn't help have a look at the source code, chrome_ex_oauth.js.

您必须使用'body'而不是'parameters':

var request = {
  'method': 'POST',
  'body': {

调试

为了找到原因,我按照以下步骤操作(注释了我的想法):

Debugging

In order to find the cause, I followed these steps (annotated my thoughts):

  1. 显然,帖子正文为空.因此,API的实现一定是错误的.
  2. Ctrl + F sendSignedRequest:

ChromeExOAuth.prototype.sendSignedRequest = function(url, callback, opt_params) {
  var method = opt_params && opt_params['method'] || 'GET';
  var body = opt_params && opt_params['body'] || null;
  var params = opt_params && opt_params['parameters'] || {};
  var headers = opt_params && opt_params['headers'] || {};
var signedUrl = this.signURL(url, method, params);
// Hmm...? Where is `params` being passed...? ChromeExOAuth.sendRequest(method, signedUrl, headers, body, function (xhr) { if (xhr.readyState == 4) { callback(xhr.responseText, xhr); } }); };

  • signURL不会修改params,所以这不是问题.
  • Ctrl + F sendRequest:

  • signURL doesn't modify params, so that's not a problem.
  • Ctrl + F sendRequest:

    ChromeExOAuth.sendRequest = function(method, url, headers, body, callback) {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function(data) {
        callback(xhr, data);
      }
      xhr.open(method, url, true);
      if (headers) { . . . }
      xhr.send(body); // <-- !!!
    };

  • 知道了!必须使用 body代替parameters.
  • body变量回溯到 request['body'] (请参见2).
  • Got it! body has to be used instead of parameters.
  • Backtracks the body variable to the request['body'] (see 2).
  • 这篇关于向Chrome扩展程序内的Tumblr API发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-07 00:28