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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图