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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理