简体   繁体   中英

Golang program for retrieving google+ Domain profile returns 403 Forbidden

Can you please help me with the below problem.

I am just writing a simple code to retrieve my google+ Domain user profile. 1. I am using a google+ Domain Account with the domain name spaceandhow.com

  1. I have provided all the privileges as listed in https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

  2. Both Google+ API and Google+ Domain Api are enabled on the account.

This is the function I have written.

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")   
}

This is the error message i get

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

Forbidden error will occur due to token problem with plus API. After you got token for the person you are authenticating, you can simply write code as below to get people information.

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))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM