我们可以在GCP中自动创建APIKey吗?还是可以将Google地图与gcp服务帐户一起使用?

时间:2019-07-16 11:40:52

标签: google-maps go google-cloud-platform service-accounts google-iam

我试图通过任何API或terraform或google客户端库自动创建API密钥。 我通过了Google服务,发现有服务

https://console.cloud.google.com/marketplace/details/google/apikeys.googleapis.com

apikeys.googleapis.com

但是我找不到任何与此相关的文档?有人可以帮我弄这个吗?或告诉我目前是否可能

然后,我尝试使用简单的JavaScript地图和代理服务器(带有Google客户端向Google Maps API发送请求的代理服务器)创建go lang应用程序。 有什么方法可以在Google地图上使用服务帐户?

map_server / static / index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
      }
    </script>
    <script src="/maps/api/js?callback=initMap"
    async defer></script>
  </body>
</html>

map_server / main.go

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"

    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
)

func main() {
    client, err := google.DefaultClient(oauth2.NoContext, "https://www.googleapis.com/auth/devstorage.full_control", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/cloud-platform")
    if err != nil {
        fmt.Printf("Error while creating default client")
        return
    }
    client.Timeout = time.Second * 15

    http.Handle("/", http.FileServer(http.Dir("./static")))
    http.HandleFunc("/maps/api/js", func(w http.ResponseWriter, r *http.Request) {
        url := "https://maps.googleapis.com/maps/api/js?callback=initMap"
        resp, err := client.Get(url)
        if err != nil {
            fmt.Printf("%s", err)
        }
        fmt.Println("response Status:", resp.Status)
        fmt.Println("response Headers:", resp.Header)
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Fprintf(w, string(body))
    })
    http.ListenAndServe(":80", nil)
}

0 个答案:

没有答案