2017-02-10 48 views
0

我試圖返回在YouTube API的訪問令牌,但不知道如何在JQuery的HTTP Post在JQuery的

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 
格式化這個POST

感謝您的幫助!

編輯:這是到目前爲止我的代碼

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) 
} 
}) 

}; 
+2

http://stackoverflow.com/questions/5771878/jquery-ajax-request-change-user-agent + https://api.jquery.com/jquery.post/應該爲你足以用作參考。你有任何目前嘗試的代碼顯示在這裏? – G0dsquad

+0

@ G0dsquad迄今爲止添加了我的代碼的編輯,感謝您的回覆 –

+0

您需要發佈參數,而不僅僅是將它們粘貼在http get請求的末尾。 – DaImTo

回答

2

您可以使用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"); 
    } 
}); 
0

我想你缺少一個?

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