balonso | 2017-10-05 17:09:27 UTC | #1
I'm updating my application to use PureCloudPlatform.Client.V2 v19.0.0 and ran into a snag when attempting to add a subscription to the Notification handler. Here's the code:
Channel _channel = notificationsapi.PostNotificationsChannels(); var handler = new NotificationHandler(channel);
handler.AddSubscription($"v2.users.{me.Id}.presence", typeof(UserPresenceNotification)); handler.AddSubscription($"v2.users.{me.Id}.conversations.calls", typeof(ConversationNotification));
The logic fails at the first AddSubscription since the SDK's underlying PostNotificationsChannelSubscriptionsWithHttpInfo() fails since the Configuration.AccessToken is not set.
How can I pass the Bearer token to this API if I'm using NotificationHandler ?
Thanks Brian Alonso
tim.smith | 2017-10-05 17:09:18 UTC | #2
You need to authorize your application before creating NotificationHandler instances. See the section Using the Library in the documentation for information about authorizing using the SDK.
balonso | 2017-10-06 14:18:19 UTC | #3
Yes, the application is authorized and has the bearer token. However, the NotificationHandler does not have a Configuration Parameter like the NotificationsApi, so I'm not sure how to pass the NotificationHandler the bearer token
tim.smith | 2017-10-06 15:30:02 UTC | #4
The NotificationHandler uses the NotificationsApi. If you've authorized your application and the SDK can make API requests, so can the NotificationHandler. Here's my test app to confirm it works correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ININ.PureCloud.OAuthControl;
using Newtonsoft.Json;
using PureCloudPlatform.Client.V2.Api;
using PureCloudPlatform.Client.V2.Extensions.Notifications;
using PureCloudPlatform.Client.V2.Model;
namespace dotnetsdktest
{
class Program7
{
[STAThread]
static void Main(string[] args)
{
try
{
// Create form
var form = new OAuthWebBrowserForm();
// Set settings
form.oAuthWebBrowser1.ClientId = "babbc081-0761-4f16-8f56-071aa402ebcb";
form.oAuthWebBrowser1.RedirectUriIsFake = true;
form.oAuthWebBrowser1.RedirectUri = "http://localhost:8080";
// Open it
var result = form.ShowDialog();
Console.WriteLine($"Result: {result}");
Console.WriteLine($"AccessToken: {form.oAuthWebBrowser1.AccessToken}");
PureCloudPlatform.Client.V2.Client.Configuration.Default.AccessToken = form.oAuthWebBrowser1.AccessToken;
var usersApi = new UsersApi();
var _me = usersApi.GetUsersMe();
var handler = new NotificationHandler();
handler.AddSubscription($"v2.users.{_me.Id}.presence", typeof(UserPresenceNotification));
handler.AddSubscription($"v2.users.{_me.Id}.conversations", typeof(ConversationNotification));
handler.NotificationReceived += (data) =>
{
Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
if (data.GetType() == typeof(NotificationData<UserPresenceNotification>))
{
var presence = (NotificationData<UserPresenceNotification>)data;
Console.WriteLine($"New presence: {presence.EventBody.PresenceDefinition.SystemPresence}");
}
else if (data.GetType() == typeof(NotificationData<ConversationNotification>))
{
var conversation = (NotificationData<ConversationNotification>)data;
Console.WriteLine($"Conversation: {conversation.EventBody.Id}");
}
};
Console.WriteLine("Websocket connected, awaiting messages...");
Console.WriteLine("Press any key to remove conversations subscription.");
Console.ReadKey(true);
handler.RemoveSubscription($"v2.users.{_me.Id}.conversations");
Console.WriteLine("Conversations subscription removed, awaiting messages...");
Console.ReadKey(true);
} catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey(true);
}
}
}
}
balonso | 2017-10-06 18:00:16 UTC | #5
Thanks, Tim.
I was newing up a V2 config and setting the AccessToken as in the following:
PureCloudPlatform.Client.V2.Client.Configuration _config = new PureCloudPlatform.Client.V2.Client.Configuration(); _config.AccessToken = bearerToken;
Doing the following instead:
PureCloudPlatform.Client.V2.Client.Configuration.Default.AccessToken = bearerToken;
works fine.
Thanks !
Brian
system | 2017-11-06 18:00:17 UTC | #6
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 1887