尝试使用XMLHttpsrequest获取Imgur的访问令牌

时间:2019-01-02 19:20:56

标签: javascript xmlhttprequest form-data imgur

我正在尝试使用XMLHttpsrequest访问我的imgur帐户的访问令牌。 imgur文档指出,它需要一个post方法,该方法的主体包含refresh_token,client_id,client_secret和grant_type(即“ refresh_token”),该文档中的示例显示您可以创建一个FormData并使用.send( )函数,但是当我在自己的代码中执行该操作时(在gen.send(body)中看到),我得到了错误:throw new TypeError('“ string”必须是字符串,Buffer或ArrayBuffer')。我不知道我在这里做错了什么,或者如何解决它,任何帮助将不胜感激。

function generateAT(){
  var gen = new XMLHttpRequest();
  var body = new FormData();
  body.append('refresh_token', refreshToken);
  body.append('client_id', clientID);
  body.append('client_secret', clientSecret);
  body.append('grant_type', "refresh_token");
  
  gen.onreadystatechange = function(){
    if (gen.readyState == 4) {
      if(gen.status == 200){
        console.log("works");
      }
      else if(gen.status == 400) {
         console.log('There was an error processing the token.');
      }
      else {
       console.log('something else other than 200 was returned');
      }
    }
    else{
      console.log("not working");
    }
  }
  gen.open("POST", "https://api.imgur.com/oauth2/token", true);
  gen.send(body);

}

0 个答案:

没有答案