Reputation: 131
I would like to GET information(data) from API and display it. Data is fetched from Big Query by using API.
Currently, I have written the code which potentially supposes to display information from API, but I'm not sure how to use service account as environmental.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
response, err := http.Get("https://www.googleapis.com/bigquery/v2/projects/PROJECT_ID/queries/JOB_ID")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
}
}
Expected result should be just to display data from API, then I will need to create an API which can be accessible without authentication with parameters (as GET Method)
P.S. Here is the link to API - https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/getQueryResults
Upvotes: 0
Views: 537
Reputation: 116938
If you check the documentation you will notice it stats Jobs: getQueryResults It states that the method you are calling requires that you be authenticated with one of the following scopes.
The data you are trying to access is private user data you must be authenticated in order to access private user data. You dont appear to be trying to authenticate in any manner.
The service account credentalis you create should be uesd in your code to send an authorization request to google
You can find some information here on how to authenticate with a service account. introduction to authentication
Enable credentials
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"
code
// Sample bigquery-quickstart creates a Google BigQuery dataset.
package main
import (
"fmt"
"log"
// Imports the Google Cloud BigQuery client package.
"cloud.google.com/go/bigquery"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "YOUR_PROJECT_ID"
// Creates a client.
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the name for the new dataset.
datasetName := "my_new_dataset"
// Creates the new BigQuery dataset.
if err := client.Dataset(datasetName).Create(ctx, &bigquery.DatasetMetadata{}); err != nil {
log.Fatalf("Failed to create dataset: %v", err)
}
fmt.Printf("Dataset created\n")
}
Upvotes: 2