尝试通过Javascript使用PATCH更新Google网上论坛

时间:2013-02-27 18:50:35

标签: google-groups-api

我正在尝试使用PATCH更新Google群组,但无法让它工作。我不确定它是否是语法问题或者无法通过Javascript完成。我可以使用OAUTH成功执行“get”操作,但不能成功执行PATCH。

  1. 我应该在setRequestHeader“授权”中使用OAuth或Bearer(两者似乎都适用于GET,并且在“PATCH”期间以相同的方式失败。
  2. get工作正常。 PATCH上的错误是{“error”:{“errors”:[{“domain”:“global”,“reason”:“invalid”,“message”:“Permission denied:无法从Groups目录中隐藏”。 },“code”:400,“message”:“权限被拒绝:无法从群组目录中隐藏。” }}

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
    function loadXMLDoc() {
    var xmlhttppatcher;
    var xmlhttp;
    var data;   
    if (window.XMLHttpRequest) {
    xmlhttppatcher = new XMLHttpRequest(); xmlhttppatcher.open("PATCH","https://www.googleapis.com/groups/v1/groups/nogads2%40mydomain.org", false);
    xmlhttppatcher.setRequestHeader("Content-Type", "application/json");
    xmlhttppatcher.setRequestHeader("Authorization", "OAuth " + "ya29.AHES6ZR_yljyMvWCv0gWbwIASYYp29S8rFUA-dd-YkyyjTHz");
    //xmlhttppatcher.setRequestHeader("Authorization", "Bearer " + "ya29.AHES6ZR_yljyMvWCv0gWbwIASYYp29S8rFUA-dd-YkyyjTHz");
    data = '{"replyTo": "REPLY_TO_MANAGERS"}';
    alert(data);
    xmlhttppatcher.send(data);
    alert('Your data was sent');
    document.getElementById("myDiv1").innerHTML = xmlhttppatcher.responseText;
    alert('readystate: ' + xmlhttppatcher.readyState + ' status: ' + xmlhttppatcher.status); //this should return 4 & 200
    if (xmlhttppatcher.status == 200)
    alert("The request succeeded!\n\nThe response representation was:\n\n" + xmlhttppatcher.responseText);
    else
    alert("The request did not succeed!\n\nThe response status was: " + xmlhttppatcher.status + " " + xmlhttppatcher.statusText + ".");
    }
    if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "https://www.googleapis.com/groups/v1/groups/nogads2%40mydomain.org?alt=json", false);
    xmlhttp.setRequestHeader("Authorization", "OAuth " + "ya29.AHES6ZR_yljyMvWCv0gWbwIASYYp29S8rFUA-dd-YkyyjTHz");
    xmlhttp.send();
    alert('readystate: ' + xmlhttp.readyState + ' status: ' + xmlhttp.status); //this should return 4 & 200
    document.getElementById("myDiv2").innerHTML = xmlhttp.responseText;
    if (xmlhttp.status == 200)
    alert("The request succeeded!\n\nThe response representation was:\n\n" + xmlhttp.responseText);
    else
    alert("The request did not succeed!\n\nThe response status was: " + xmlhttp.status + " " + xmlhttp.statusText + ".");
    }
    }
    </script>
    </head>
    
    <body>
    <div id="myDiv1"><h2>Let AJAX change this text (DIV1)</h2></div><br />
    <div id="myDiv2"><h2>Let AJAX change this text (DIV2)</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
    </body>
    

1 个答案:

答案 0 :(得分:0)

嘿,我遇到了同样的问题,我通过创建一个新的Groups对象[Java]来修复它,并设置了我想要更新的特定属性