I am trying to use the access token generated by the client authentication grant flow to connect to the Notifications API using golang. I know the token works because I am able to retrieve data from Platform API endpoints using it. However, when I try to pass the token in to authenticate with the Notifications API, I am getting 401 unauthorized.
In this code, auth represents the authorization response received by the grant flow. What am I doing wrong?
func WsConnect(auth *AuthResponse) {
//Set up the websocket connection
targetURI := fmt.Sprintf("wss://streaming.%s/channels/%s", config.GetConf().REGION, config.GetConf().CHANNELS)
bearer := auth.TokenType + " " + auth.AccessToken
header := http.Header{"Authorization": []string{bearer}}
c, r, err := websocket.DefaultDialer.Dial(targetURI, header)
if err != nil {
fmt.Println("Status:", r.Status, "Request:", r.Request)
logger.Fatal("Unable to connect to web socket:", err)
}
defer c.Close()
}
I logged the response and request. Here is what that looks like:
Nice. We have a golang notifications example in our backlog and assigned (it just has not been prioritized yet). I am going to ask the team member who is going to be working on the example to take the code above and see if he can figure out why you are getting a 401. If I remember correctly (it's been awhile since I did a direct auth via the headers), but I think you might have to base 64 to encode the value.
Thanks,
John Carnell
Manager, Developer Engagement
I believe I figured out why it was getting 401 unauthorized. I was adding an API endpoint at the end of this URL https://streaming.mypurecloud.com/channels, but it should have been a channel returned after requesting a channel.