【问题标题】:Http POST to a google formHttp POST 到谷歌表单
【发布时间】:2013-08-07 02:14:20
【问题描述】:

我需要对我的 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];

【问题讨论】:

    标签: http http-post google-forms


    【解决方案1】:

    发送 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
    

    【讨论】:

      【解决方案2】:

      我使用 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)
      }
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2013-10-10
            • 2019-09-14
            • 1970-01-01
            • 2015-08-22
            • 2012-03-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多