Edd*_*ddy 8 http http-post google-forms
我需要对我的谷歌表单进行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];
Run Code Online (Sandbox Code Playgroud)
发送 POST 到以下网址
预览表单时,您可以在 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);
Run Code Online (Sandbox Code Playgroud)
应该在您的控制台中打印类似的内容
Field1: entry.748645480
Field2: entry.919588971
Run Code Online (Sandbox Code Playgroud)
我使用 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)
}
Run Code Online (Sandbox Code Playgroud)