Making a POST request to Tumblr's API inside a Chrome Extension

谁说胖子不能爱 提交于 2019-12-06 11:38:04

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

You have to use 'body' instead of 'parameters':

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

Debugging

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

  1. Apparently, the post body is empty. So, the implementation of the API must be wrong.
  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); } }); };
  3. signURL doesn't modify params, so that's not a problem.
  4. 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); // <-- !!!
    };
  5. Got it! body has to be used instead of parameters.
  6. Backtracks the body variable to the request['body'] (see 2).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!