Skip to content

Instantly share code, notes, and snippets.

@akeller
Created December 12, 2018 23:16
Show Gist options
  • Save akeller/2d7b8913308401495afed257a74a7f28 to your computer and use it in GitHub Desktop.
Save akeller/2d7b8913308401495afed257a74a7f28 to your computer and use it in GitHub Desktop.
A snippet for using Watson Assistant with API key authentication
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IBM.Watson.DeveloperCloud.Services.Assistant.v1;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Connection;
using FullSerializer;
using IBM.Watson.DeveloperCloud.DataTypes;
public class WatsonAssistantApiKey : MonoBehaviour
{
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
[Header("Watson Assistant")]
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/assistant/api\"")]
[SerializeField]
private string assistantURL;
[SerializeField]
private string assistantWorkspace;
[Header("CF Authentication")]
[SerializeField]
private string assistantUsername;
[SerializeField]
private string assistantPassword;
[Header("IAM Authentication")]
[Tooltip("The IAM apikey.")]
[SerializeField]
private string assistantIamApikey;
[Tooltip("The IAM url used to authenticate the apikey (optional). This defaults to \"https://iam.bluemix.net/identity/token\".")]
[SerializeField]
private string assistantIamUrl;
#endregion
private void Start(){
Credentials asst_credentials = null;
if(!string.IsNullOrEmpty(assistantUsername) && !string.IsNullOrEmpty(assistantPassword)){
//Authenticate using username and password
asst_credentials = new Credentials(assistantUsername, assistantPassword, assistantURL);
_assistant = new Assistant(asst_credentials);
//be sure to give it a Version Date
_assistant.VersionDate = "2018-09-20";
} else if(!string.IsNullOrEmpty(assistantIamApikey)) {
//Authenticate using iamApikey
TokenOptions tokenOptions = new TokenOptions()
{
IamApiKey = assistantIamApikey,
IamUrl = assistantIamUrl
};
asst_credentials = new Credentials(tokenOptions, assistantURL);
} else {
throw new WatsonException("Please provide either username and password or IAM apikey to authenticate the service.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment