【问题标题】:HTTP Post in JQueryjQuery 中的 HTTP 发布
【发布时间】:2017-06-28 17:44:10
【问题描述】:

我正在尝试返回 youtube API 的访问令牌,但不知道如何在 JQuery 中格式化此 POST

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-length: 184
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground
client_secret=************&grant_type=refresh_token&refresh_token=1%2FPHiWsKPQXQJCNKBbTPgiR0QHugKlXp8Pd2cRlohjK80hAConmTyV5XVmg2HfO4Ag&client_id=407408718192.apps.googleusercontent.com

感谢您的帮助!

编辑:这是我目前的代码

 jQuery.ajax({
url: "https://www.googleapis.com/oauth2/v4/token/",
type: "post",
data: {
  grant_type: "refresh_token",
  refresh_token: 'token here',
  client_id: 'id here',
  client_secret: 'secret here',
  access_type: 'offline',

},
success: function(response){
  console.log(response)
}
})

};

【问题讨论】:

标签: javascript jquery youtube-api http-post


【解决方案1】:

您可以为此使用 AJAX。我认为.ajax() 是最好的方法,但您甚至可以使用.post() 方法。

使用.ajax()

$.ajax({
    method: "POST",
    url: "some.php",
    data: { name: "John", location: "Boston" }
}).done(function( msg ) {
    alert( "Data Saved: " + msg );
});

使用.post()

$.post( "test.php", { 'choices[]': [ "Jon", "Susan" ] } );

对于这两种情况,您都必须使用此设置您的用户代理(请参阅.ajaxSetup()):

$.ajaxSetup({
    beforeSend: function(request) {
        request.setRequestHeader("User-Agent","google-oauth-playground");
    }
});

【讨论】:

    【解决方案2】:

    我猜你错过了一个?

    /v4/token?client_secret=*******
    

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2012-07-06
      • 1970-01-01
      相关资源
      最近更新 更多