2016-11-14 61 views
2

this page上有一個示例,說明如何在普通的JavaScript網頁中包含Google Drive Picker。我已將示例代碼複製並粘貼到我的計算機上的文件中,然後按照說明使用我自己的API密鑰,客戶端ID和應用程序ID替換示例字符串。我在做什麼錯誤的Google Drive Picker示例代碼?

如果我使用python -m SimpleHTTPServer 8000從本地主機提供頁面並訪問該頁面,它將以兩種方式失敗。首先,彈出的Drive對話框顯示「出現錯誤!API開發人員密鑰無效。」其次,在控制檯上,我看到下面的錯誤。搜索網絡告訴我,這可能是因爲我使用的是非HTTPS連接,Google API會嘗試與驅動器建立HTTPS連接。 (相反的是他們的錯誤好像說。)

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://docs.google.com') does not match the recipient window's origin ('http://localhost:8000'). 
(anonymous) @ cb=gapi.loaded_0:46 

Uncaught ReferenceError: init is not defined 
    at onload (https://docs.google.com/picker?protocol=gadgets&origin=http%3A%2F%2Flocalho…22%7D))&rpctoken=6pstmf1ss7m2&rpcService=v8gvn97850jj&thirdParty=true:3:54) 

如果我所服務從本地主機使用this handy python script for an SSL web server頁面,訪問的頁面,它以不同的方式失敗。彈出的驅動器對話框顯示以下內容。

400. That’s an error. 

Error: origin_mismatch 
Request Details 
     proxy=oauth2relay379676703 
     immediate=false 
     scope=https://www.googleapis.com/auth/drive 
     origin=https://localhost:8000 
     response_type=token 
     redirect_uri=postmessage 
     state=912029351|0.3907265328 
     client_id=[REDACTED] 
     include_granted_scopes=true 
     jsh=m;/_/scs/apps-static/_/js/k=oz.gapi.en.eBn7I_cE2GI.O/m=__features__/am=AQ/rt=j/d=1/rs=AGLTcCNXKKlDNVVUdz0bvaxNh7fYe-hpSQ 
     gsiwebsdk=1 
That’s all we know. 

儘管這似乎表明,驅動器API不希望從本地主機聽,我必須設置爲接受從https://localhost:8000/*http://localhost:8000/*請求的應用程序的憑據(在我的谷歌開發者信息中心)。

我在做什麼錯?

回答

1

您不能將本地主機用作「原始」地址 - 這是Google API的侷限性。要解決這個問題,您可能需要使用this問題中建議的選項之一 - 使用URL縮寫器或將您的主機文件配置爲將自定義URL解析爲127.0.0.1。

+0

我不能使用URL shortener方法,因爲我的URL包含一個goo.gl不支持的端口。我按照鏈接問題中的建議編輯了我的/ etc/hosts文件,並且現在從本地加載了假的URL,因爲它應該是這樣,但是行爲與如果我簡單地使用localhost相反會發生什麼。換句話說,我原來的問題中的所有錯誤依然存在;從URL中刪除「localhost」我沒有任何改進。 – Nathan

+0

@Nathan你能提供編輯hosts文件後看到的錯誤嗎? –

+0

當我嘗試通過URL mydevserver.example.com訪問本地主機時,原始帖子中出現的兩個錯誤中的第二個錯誤是由/ etc/hosts重新路由到本地主機。我已嘗試使用http和https,在您最後評論後不久將mydevserver.example.com添加到我的Google API憑證中,然後等待30分鐘以確保其生效。 – Nathan