2016-11-04 92 views
1

我使用的API示例爲here無法在Cloud Natural Language中設置項目API

運行gcloud config list顯示我的默認項目爲project = multichoice-insights

重要的代碼摘錄顯示,我得到了默認憑據:

credentials = GoogleCredentials.get_application_default().create_scoped(
    ['https://www.googleapis.com/auth/cloud-platform'], 
) 

然而,當我嘗試使用API​​,我得到的錯誤消息:

googleapiclient.errors.HttpError: <HttpError 403 when requesting 
https://language.googleapis.com/v1beta1/documents:analyzeSentiment? 
alt=json returned "Google Cloud Natural Language API has not been used 
in project google.com:cloudsdktool before or it is disabled. Enable it 
by visiting 
    https://console.developers.google.com/apis/api/language.googleapis.com/over 
view?project=google.com:cloudsdktool then retry. If you enabled this 
API recently, wait a few minutes for the action to propagate to our 
systems and retry."> 

我認爲,這顯示使用的項目:project=google.com:cloudsdktool

我該如何將其更改爲project-name

我已經在控制檯中啓用了這個api。

回答

2

對於有此問題的人:

需要設置其環境變量。如果您使用的是virtualenvs,請按如下所示設置變量。

在文件:.virtualenvs/env_name/bin編輯文件啓動以包含以下行:

GOOGLE_APPLICATION_CREDENTIALS=/abs/path/to/credentials.json 

這credentials.json從API頁面donwloaded。完整的說明可以從here

希望這可以幫助別人。

+0

設置GOOGLE_APPLICATION_CREDENTIALS env var在我的情況下沒有解決問題......它在生產(AppEngine)中工作,但不能在開發中運行......任何其他人都有相同的問題? – Neurus

+0

嘗試使用'未設置GOOGLE_APPLICATION_CREDENTIALS'取消設置您的env var,然後嘗試使用'gcloud auth application-default login' 有趣的是看看它是否有效。 –

相關問題