2013-10-09 120 views
0

我有一個應用程序引擎應用程序,它導入了一個jar。在該jar中,我使用 GoogleClientSecrets.load() 加載client_secrets.json文件以使用BigQuery進行身份驗證。顯然,當我在本地部署應用程序時,App Engine不喜歡從磁盤上的某個位置讀取文件。我假設如果我把證書放在WEBINF文件夾中,它會起作用,但還沒有測試過,但是任何人都可以輕鬆地訪問這個文件。哪裏是放置證書的最佳地點,以及如何從App Engine應用程序訪問它們?App Engine憑據問題

謝謝你的幫助!

這些建議有助於解決讀取文件時遇到的問題。如何寫入文件?我正在使用存儲憑證文件的FileCredentialStore。

我相信這行是造成一個問題: FileCredentialStore variantStoreCredentialManager = new FileCredentialStore(expectedClientFile,jsonFactory); 且誤差 java.security.AccessControlException:訪問被拒絕(「java.io.FilePermission中的」文件路徑「寫入」)

public Bigquery createAuthorizedClient() throws IOException { 
    Credential authorization = new GoogleCredential(); 
    if (clientID == null) { 
     authorization = createWebAuthenticatedClientCredential(); 
    } else { 
     String expectedFileLocation = CREDENTIAL_FILE_PATH; 
     File expectedClientFile = new File(expectedFileLocation); 
     if (! expectedClientFile.exists()) { 
      // this is a known issue, the credential store will blow up if the file doesn't exist. So create it with an 
      // empty json ({ }) 
      createClientFile(expectedClientFile); 
     } 
     FileCredentialStore variantStoreCredentialManager = new FileCredentialStore(expectedClientFile,jsonFactory); 
     GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder(); 
     credentialBuilder.setJsonFactory(jsonFactory); 
     credentialBuilder.setClientSecrets(clientSecrets); 
     credentialBuilder.setTransport(transport); 
     authorization = credentialBuilder.build(); 
     boolean loadedSuccessfully = variantStoreCredentialManager.load(clientID,authorization); 
     if (! loadedSuccessfully ) { 
      authorization = createWebAuthenticatedClientCredential(); 
      variantStoreCredentialManager.store(clientID, authorization); 
     } 
    } 

    return new Bigquery(transport, jsonFactory, authorization); 
} 
+1

'/ WEB-INF'是放置這些數據的正確位置。訪問它作爲資源,而不是文件:http://stackoverflow.com/questions/4340653/file-path-to-resource-in-our-war-web-inf-folder –

+0

謝謝你的回覆 – Tad

回答

1

沒有,/WEB-INF文件夾的內容是私有的應用程序代碼,無法訪問通過HTTP(= servlet容器不支持嘗試訪問WEB-INF文件夾中的數據的請求)。

使用此片段讀取文件的內容/WEB-INF文件夾中:

InputStream is = getServletContext().getResourceAsStream("/WEB-INF/"+filename); 

然後閱讀使用methods for reading InputStreams的一個流。

+0

我看到。有沒有辦法做到這一點,而不是在我的應用程序引擎項目中使該文件成爲資源?由於它是我的jar進行身份驗證,而不是app引擎應用程序,我認爲app引擎應用程序不應該知道這個文件。那可能嗎? – Tad

+0

恩,https://developers.google.com/appengine/kb/java#writefile是否意味着寫入文件是不可能的? – Tad

+0

是的,文件系統是隻讀的。 –