当前位置 : 主页 > 网络推广 > seo >

用于检索谷歌域名配置文件的Golang程序返回403 Forbidden

来源:互联网 收集:自由互联 发布时间:2021-06-16
你可以帮我解决下面的问题. 我只是编写一个简单的代码来检索我的谷歌域用户配置文件. 1.我正在使用域名为spaceandhow.com的谷歌域帐户 我已提供https://developers.google.com/identity/protocols/O
你可以帮我解决下面的问题.

我只是编写一个简单的代码来检索我的谷歌域用户配置文件.
1.我正在使用域名为spaceandhow.com的谷歌域帐户

>我已提供https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority中列出的所有权限
>该帐户已启用Google API和Google Domain Api.

这是我写的功能.

func (p *GoogleAUTH) sendGoogleAuthReq(){

    fmt.Println("GoogleAUTH package: Enter sendGoogleAuthReq")


    data, err := ioutil.ReadFile("D:\\Cygwin\\home\\praprasa\\pragna2.json")
    if err != nil {
        fmt.Printf("ReadFile error: %s", err)
    }


    conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/plus.me")
    if err != nil {
        fmt.Println("json error")

    }

    client := conf.Client(oauth2.NoContext)

    resp, err := client.Get("https://www.googleapis.com/plusDomains/v1/people/me")
    if err != nil {
        fmt.Printf("GoogleAUTH package: request execution failed: %s", err)
        return
    }
    defer resp.Body.Close()

    fmt.Println("GoogleAUTH package: response Status:", resp.Status)
    fmt.Println("GoogleAUTH package: response Headers:", resp.Header)

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Printf("GoogleAUTH package: cannot fetch token: %v", err)
        return
    }

    fmt.Println("GoogleAUTH package: response Body:", string(body))

    fmt.Println("GoogleAUTH package: Exit sendGoogleAuthReq")   
}

这是我得到的错误消息

GoogleAUTH package: response Status: 403 Forbidden
GoogleAUTH package: response Headers: map[Vary:[Origin X-Origin] X-Frame-Options:[SAMEORIGIN] X-Xss-Protection:[1; mode=block] Cache-Control:[private, max-age=0] Server:[GSE] Date:[Sat, 29 Aug 2015 07:55:47 GMT] Content-Type:[application/json; charset=UTF-8] Expires:[Sat, 29 Aug 2015 07:55:47 GMT] X-Content-Type-Options:[nosniff] Alternate-Protocol:[443:quic,p=1] Alt-Svc:[quic=":443"; p="1"; ma=604800]]
GoogleAUTH package: response Body: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}
由于加上API的令牌问题,将发生禁止错误.获得正在验证的人的令牌后,您只需编写如下代码即可获取人员信息.

func (p *GoogleAUTH) sendGoogleAuthReq(){
   baseUrl := "https://www.googleapis.com/plus/v1/people/me"
   authbear := "Bearer "
   authbear += <access_token>
   cli := &http.Client{}
   req, _ := http.NewRequest("GET", baseUrl, nil)
   req.Header.Set("Authorization", authbear)
   res, _ := cli.Do(req)
   bo, _ := ioutil.ReadAll(res.Body)
   fmt.Println(string(bo))
}
网友评论