Http POST到谷歌表格

时间:2013-08-06 07:20:21

标签: http http-post google-form

我需要对我的谷歌表单进行Http POST,并按照建议(http://productforums.google.com/forum/#!topic/docs/iCfNwOliYKY)创建了这样的URL语法:

https://docs.google.com/forms/d/MY_FORM_ID&entry.2087820476=hello&entry.261928712=dear&submit=Submit

最终我将从iOS和Android进行POST,但我只是在浏览器中对它进行测试,但它不起作用。我收到了Google云端硬盘的回复,上面写着“抱歉,您请求的文件不存在。”并且该条目不会进入我的回复电子表格。

我错过了什么?我到处看,但似乎没有回答这个问题。

编辑:

我猜有时最好直接在目标平台上进行测试。以下代码适用于iOS:

NSString *post = @"&key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://docs.google.com/forms/d/MY_FORM_ID/formResponse"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

4 个答案:

答案 0 :(得分:3)

将POST发送到以下网址

https://docs.google.com/forms/d/e/[FORM_ID]/formResponse?entry.1098499744=AnswerField1&entry.1090138332=AnswerField2

预览表单时,您可以在网址中找到FORM_ID。

要获取字段ID,请检查每个输入标记的“name”属性。或者在表单页面的控制台中运行以下脚本。

function loop(e){
if(e.children)
    for(let i=0;i<e.children.length;i++){
        let c = e.children[i], n = c.getAttribute('name');
        if(n) console.log(`${c.getAttribute('aria-label')}: ${n}`);
        loop(e.children[i]);
     }
}; loop(document.body);

应该在你的控制台中打印类似的东西

Field1: entry.748645480
Field2: entry.919588971

答案 1 :(得分:0)

我使用Swift完成了这项任务。请在此回购中查看:https://github.com/goktugyil/QorumLogs

以下是如何设置它的教程: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

继承代码:

private static func sendError(#text: String) {
    var url = NSURL(string: formURL)
    var postData = form1Bar + "=" + text
    postData += "&" + form2Bar + "=" + "anothertext"                
    postData += "&" + form3Bar + "=" + "anothertext" 
    postData += "&" + form4Bar + "=" + "anothertext" 

    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
    var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}

答案 2 :(得分:0)

答案 3 :(得分:0)

如果您使用Google Chrome开发人员工具或任何浏览器的工具查看表单,您会看到每个控件都有一个名称。示例:name='entry.123456789'

在C#中,您可以在以下表单上设置答案:

private static void PostToForm()
{
    WebClient client = new WebClient();

    var keyValue = new NameValueCollection();
    keyValue.Add("entry.123456789", "My answer #1");
    keyValue.Add("entry.987654321", "My answer #2");

    Uri uri = new Uri("https://docs.google.com/forms/d/[form id]/viewform");

    byte[] response = client.UploadValues(uri, "POST", keyValue);
}

您还可以在网址中设置答案:

https://docs.google.com/forms/d/[form id]/viewform?entry.123456789=Answer1&entry.987654321=Answer2