dtrpv60860 2019-02-14 13:57
浏览 299
已采纳

使用服务帐户从Google REST API获取数据

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
", 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

  • 写回答

1条回答 默认 最新

  • dqf67993 2019-02-14 14:20
    关注

    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.

    enter image description here

    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
    ")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?