3 回答

TA貢獻1890條經(jīng)驗 獲得超9個贊
1.創(chuàng)建或選擇一個項目。
gcloud projects create nat-lan-api
gcloud config set project nat-lan-api
2.啟用計費。
gcloud alpha billing projects link nat-lan-api --billing-account XXXXXX-XXXXXX-XXXXXX
3.為該項目啟用 Google Natural Language API。
gcloud services enable language.googleapis.com
3.創(chuàng)建一個服務帳戶。
gcloud iam service-accounts create natural-language-api --description "natural-language-api" --display-name "natural-language-api"
gcloud iam service-accounts list
4.以 JSON 格式下載私鑰。
gcloud iam service-accounts keys create key.json --iam-account natural-language-api@nat-lan-api.iam.gserviceaccount.com
5. 將環(huán)境變量 GOOGLE_APPLICATION_CREDENTIALS 設置為包含您的服務帳戶密鑰的 JSON 文件的路徑。此變量僅適用于您當前的 shell 會話,因此如果您打開一個新會話,請再次設置該變量。
export GOOGLE_APPLICATION_CREDENTIALS="/Users/user/folder/key.json"
6.安裝客戶端庫。
pip install --upgrade google-cloud-language
7.分析一些文本。
cat natural.py
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
text = u'Hello, world!'
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
8.測試。
python natural.py
#Text: Hello, world!
#Sentiment: 0.30000001192092896, 0.30000001192092896

TA貢獻1873條經(jīng)驗 獲得超9個贊
是的,Google 允許從您的本地平臺使用 API。步驟如下
您需要創(chuàng)建一個具有適當權限的服務帳戶。
創(chuàng)建該服務帳戶的私鑰并將其保存在本地計算機中。
使用該私鑰從 jwt.io 站點生成 jwt 令牌。
使用該 jwt 調用訪問令牌 API 以獲取訪問令牌。
使用訪問令牌調用語言處理 API。
我嘗試過使用 Java 技術的 google DB 遷移 API。你可以參考我的代碼。
https://github.com/itssanjib/google-cloud-poc/tree/master/gcp-db-migration-poc
如果需要任何幫助,請告訴我。

TA貢獻1848條經(jīng)驗 獲得超10個贊
您可以使用自然語言客戶端庫從 Python 調用 API:
https://cloud.google.com/natural-language/docs/quickstart-client-libraries
由于您不會從 Google Cloud Platform 調用 API,因此您需要創(chuàng)建一個服務帳戶并使用它進行身份驗證。
添加回答
舉報