Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Google cloud API from local Project not Hosted on Google cloud platform

I want to use google cloud natural language API from local python code. Because of project constraints I can not run my code on GCP Platform. I have google cloud account and credits to enable and use the API. Does google allow to use the API to run on local platforms. Any sample code will be helpful.

like image 948
Anil Kumar Avatar asked Feb 12 '20 12:02

Anil Kumar


People also ask

What is needed to access GCP APIs in your project?

To use Google Cloud APIs in your applications, you first need to have a Google account. This allows you to use Google developer products, including Google Cloud console, gcloud CLI, Cloud Logging, and Cloud Monitoring.


2 Answers

1.Create or select a project.

gcloud projects create nat-lan-api
gcloud config set project nat-lan-api

2.Enable billing.

gcloud alpha billing projects link  nat-lan-api  --billing-account XXXXXX-XXXXXX-XXXXXX

3.Enable the Google Natural Language API for that project.

gcloud services enable  language.googleapis.com

3.Create a service account.

gcloud iam service-accounts create natural-language-api  --description "natural-language-api"  --display-name "natural-language-api"
gcloud iam service-accounts list

4.Download a private key as JSON.

gcloud iam service-accounts keys create key.json   --iam-account [email protected] 

5.Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

export GOOGLE_APPLICATION_CREDENTIALS="/Users/user/folder/key.json"

6.Install the client library.

pip install --upgrade google-cloud-language

7.Analyze some text.

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.Test.

python natural.py 
#Text: Hello, world!
#Sentiment: 0.30000001192092896, 0.30000001192092896
like image 152
marian.vladoi Avatar answered Oct 07 '22 01:10

marian.vladoi


Yes, Google allow to use the API from your local platforms. The steps is as follows

  1. You need to create a service account with the proper permission.
  2. Create a private key of that service account and keep it in your local machine.
  3. Using that private key generate the jwt token from jwt.io site.
  4. Use that jwt to call the access token API to get the access token.
  5. Use the access token call the language processing API.

I have tried for google DB migration API using Java technology. You can refer my code.

https://github.com/itssanjib/google-cloud-poc/tree/master/gcp-db-migration-poc

Please let me know, if any assistance is required.

like image 31
Sanjib Pal Avatar answered Oct 07 '22 03:10

Sanjib Pal