2
votes

I want to build a simple rest API using Google Cloud Datastore and I can't access my data

First, I am new with Google Cloud Platform but this is not going to stop me until I find a solution on how to make a rest API call with Google Cloud Datastore.

I use Angular 4 in as front end and I want to make a simple HTTP request.

This is my data in Google Cloud Datastore

enter image description here

This is my api.service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';




@Injectable()
export class ApiService {

  private apiUrl1 = 'https://jsonplaceholder.typicode.com/posts';
  private apiUrl2= 'https://datastore.googleapis.com/v1/projects/project-test:runQuery?key=dsdsdsdsdsdsdsdsdsdsdsds';

  data: any = {};

  constructor(
    private http: Http
  ) { }

  getData1() {
    return this.http.get(this.apiUrl1)
    .map((res: Response) => res.json());  <====== this works
  }

  getData2() {
    const body = {
      "query": {}
    };

    return this.http.post(this.apiUrl2, body)
    .map((res: Response) => res.json()); <====== this dont works
  }

}

I have this error

POST https://datastore.googleapis.com/v1/projects/project-test:runQuery?key=1b169abfa8b87ewewqewewqewqewqewqewqe 401 ()

Plese some expert tell me if I can access Google cloud datastore with rest API If yes why I have error 401 and why I need OAuth2

"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",↵ "status": "UNAUTHENTICATED"↵ }↵}↵"

How the users can access my data. Some data is going to be publicly available

Any example how to access Cloud datastore with rest API is going to help us. I search the entire internet there is nothing . 0 Youtube videos And all documentations is with App engine . i use Firebase hosting - angular 4 and for the queries i want cloud datastore

1

1 Answers

1
votes

TL;DR - API keys will not work with Cloud Datastore APIs. You should instead use OAuth 2.0 access tokens from either user credentials or service accounts.

API key limitations

Based on the API key documentation for Cloud APIs, only the following 4 Cloud services support API keys and Cloud Datastore is not one of them. They are usually only used with APIs which do not require access to private user data.

Google Cloud Platform supported authentication mechanism

In addition to API keys, GCP supports authentication using user account credentials and service accounts. They are explained in detail here.

Using OAuth 2.0 to invoke Google APIs

Once you have either a user account credential or a service account credentials, using OAuth 2.0 you need to obtain the access token. The process is described here. I did find these steps specific to javascript which I feel might be of most help to your use case.

The example in that page again mentions API key (which will not work for your use case) and I could not find any Angular specific example which uses service accounts. If you're using Node.js, there is a google-auth library for Node.js which takes care of all the authentication when using service accounts. The library also supports Application Default Credentials.

Passing OAuth 2.0 credentials

Once you have obtained the OAuth 2.0 access token (aka bearer token), you can pass it using one of these 3 query parameters in the Cloud Datastore API request:

  • access_token
  • oauth_token
  • bearer_token

Example using command line

echo '{gqlQuery: {queryString: "SELECT * FROM Task"}}' | http POST https://datastore.googleapis.com/v1/projects/PROJECT_NAME:runQuery?bearer_token=YOUR_OAUTH_TOKEN

If you have gcloud installed you can get the current access token using:

# Prints the access token
gcloud auth print-access-token
# Use the access token to send the request
echo '{gqlQuery: {queryString: "SELECT * FROM Task"}}' | http POST https://datastore.googleapis.com/v1/projects/PROJECT_NAME:runQuery?bearer_token=$(gcloud auth print-access-token)