2010-09-28 88 views

回答

0

摘要:您不能修改,因爲Google有closed this issue as WontFix,這可能會有一段時間。有些解決方法可以嘗試實現相同的最終結果,但按照現狀,您無法使用Google Maps Data API上傳KML文件。

龍版本:

沒有任何Python代碼要做到這一點,但Google Maps Data API允許您使用一系列HTTP請求的做到這一點。有關如何執行此操作的文檔,請參閱Developers Guide的HTTP協議部分中的Uploading KML。所以一個可能的Python解決方案是在標準庫中使用類似httplib的東西來爲你做適當的HTTP請求。

經過各種編輯和您在評論中的反饋,以下是一個腳本,它通過命令行使用Google用戶名和密碼(小心如何使用它!),通過製作ClientLogin authentication request來獲得authorization_token變量。使用有效的用戶名和密碼,身份驗證令牌可用於Authorization標題中,用於將KML數據發佈到Maps Data API。

#!/usr/bin/env python 
import httplib 
import optparse 
import sys 
import urllib 

class GoogleMaps(object): 
    source = "daybarr.com-kmluploader-0.1" 

    def __init__(self, email, passwd): 
     self.email = email 
     self.passwd = passwd 
     self._conn = None 
     self._auth_token = None 

    def _get_connection(self): 
     if not self._auth_token: 
      conn = httplib.HTTPSConnection("www.google.com") 
      params = urllib.urlencode({ 
       "accountType": "HOSTED_OR_GOOGLE", 
       "Email": self.email, 
       "Passwd": self.passwd, 
       "service": "local", 
       "source": self.source, 
      }) 
      headers = { 
       "Content-type": "application/x-www-form-urlencoded", 
       "Accept": "text/plain", 
      } 
      conn.request("POST", "/accounts/ClientLogin", params, headers) 
      response = conn.getresponse() 
      if response.status != 200: 
       raise Exception("Failed to login: %s %s" % (
        response.status, 
        response.reason)) 
      body = response.read() 
      for line in body.splitlines(): 
       if line.startswith("Auth="): 
        self._auth_token = line[5:] 
        break 
      if not self._auth_token: 
       raise Exception("Cannot find auth token in response %s" % body) 
     if not self._conn: 
      self._conn = httplib.HTTPConnection("maps.google.com") 
     return self._conn 

    connection = property(_get_connection) 

    def upload(self, kml_data): 
     conn = self.connection 
     headers = { 
      "GData-Version": "2.0", 
      "Authorization": 'GoogleLogin auth=%s' % (self._auth_token,), 
      "Content-Type": "application/vnd.google-earth.kml+xml", 
     } 
     conn.request("POST", "/maps/feeds/maps/default/full", kml_data, headers) 
     response = conn.getresponse() 
     if response.status != 200: 
      raise Exception("Failed to upload kml: %s %s" % (
       response.status, 
       response.reason)) 
     return response.read() 

if __name__ == "__main__": 
    parser = optparse.OptionParser() 
    parser.add_option("-e", "--email", help="Email address for login") 
    parser.add_option("-p", "--passwd", help="Password for login") 
    options, args = parser.parse_args() 
    if not (options.email and options.passwd): 
     parser.error("email and passwd required") 
    if args: 
     kml_file = open(args[0], "r") 
    else: 
     kml_file = sys.stdin 
    maps = GoogleMaps(options.email, options.passwd) 
    print maps.upload(kml_file.read()) 

不幸的是,使用有效的登錄憑據,即使獲得令牌的有效授權和使用的文檔中給出恰好包含例如有效的KML文件,該API響應KML後用400 Bad Request。顯然這是一個已知問題(2010年7月22日報道的2590),因此如果您希望Google進行修復,請投票發表評論。

在此期間,沒有這個bug固定的,你可以嘗試

  1. 而不上傳KML創建地圖,然後上傳KML適當的功能,如comment #9 on the issue來自谷歌,建議當他們證實,該bug存在。
  2. uploading XMLuploading CSV而不是KML如果這些方法支持您需要完成的操作
  3. 擺弄您的KML數據的格式。 Google Group for the API中的This post表明這可能有幫助,但看起來很複雜。

好運

+0

對不起,但我得到一個 400錯誤的請求 壞請求 ,我不知道如何調試這一點。 – 2010-10-06 19:24:49

+0

@Jörg拜爾奇怪。由於某種原因,如果請求不是有效的HTTP請求,通常只會得到400。我已經更新了我的答案,請給腳本一下,應該可以運行。 – Day 2010-10-06 20:31:59

+0

我仍然收到「400錯誤請求」。我相信,auth-token是可以的,因爲當我修改新生成的令牌時,我得到了401。 – 2010-10-08 18:37:30

相關問題