401 unauthorized when connecting to Notifications API using valid token (golang)

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:

Status: 401 Unauthorized 
Request: &{GET https://streaming.mypurecloud.com/channels/v2.telephony.providers.edges HTTP/1.1 1 1 map[Authorization:[bearer 123456-snipped-for-security] Connection:[Upgrade] Sec-WebSocket-Key:[123456-snipped-for-security==] Sec-WebSocket-Version:[13] Upgrade:[websocket]] <nil> <nil> 0 [] false streaming.mypurecloud.com map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc000012088}


I believe I got this working (mostly). I ditched trying to adapt the code in listenchannel.go, and began stepping through the raw steps here: https://developer.genesys.cloud/api/rest/v2/notifications/notification_service#connect-to-the-notification-service. Now I seem to be making progress.

Hi Dan,

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.

I've abandoned the above code since working through the raw grant flow steps and following the steps in https://developer.genesys.cloud/api/rest/v2/notifications/notification_service#connect-to-the-notification-service

Thanks,
Dan

Hi Dan,

Glad it was worked out.

Thanks,
John

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.