11

我需要对我的 google 表单进行 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 Drive 回复,上面写着“抱歉,您请求的文件不存在”。并且该条目不会进入我的回复电子表格。

我错过了什么?我到处看了看,但似乎没有任何东西可以回答这个问题。

编辑:

我想有时最好直接在您的目标平台上进行测试。以下代码适用于 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

4 回答 4

6

向以下网址发送 POST

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

预览表单时,您可以在 url 中找到您的 FORM_ID。

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

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
于 2017-11-22T21:49:49.263 回答
1

如果您使用 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);
}

您还可以在 URL 中设置答案:

https://docs.google.com/forms/d/[form id]/viewform?entry.123456789=Answer1&entry.987654321=Answer2
于 2015-11-23T08:44:17.627 回答
1

我使用 Swift 完成了这项任务。在这个 repo 中查看: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)
}
于 2015-09-17T10:22:27.077 回答
1

尝试调试并检查您的最终 URL,它应该是这样的 https://docs.google.com/forms/d/YOUR_FORM_ID/formResponse?entry.1748727384=test&entry.1949164265=test&submit=Submit

于 2015-10-18T20:25:32.900 回答