在 Unity 2018.2.0b3 发行说明中提及TLS 1.2已添加并适用于所有平台。当我们测试它时,它仍然没有工作并抛出一些错误。相同的代码在 .Net application 4.5 framework 中工作。
错误:SecureChannelFailure(发生了一个或多个错误。)UnityEngine.Debug:Log(Object)CheckTLS:GetAuth()(在Assets / CheckTLS.cs:31)UnityEngine.EventSystems.EventSystem:Update()
是否存在依赖关系或任何特定设置,请帮助解决此问题。
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace SmartMeterCalculations
{
class Authentication
{
public string AccessToken;
public string TokenType;
public string ExpiresIn;
public DateTime ExpiryDate;
public string Scope;
public string Ext_Attr_Enhancer;
public string JTI;
}
class Reading
{
public DateTime Date;
public Double Value;
}
class APICalls
{
public static Authentication AuthenticateAPI(
string OauthURL,
string ClientId,
string ClientSecret)
{
Authentication authentication = null;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(OauthURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json";
WebProxy webProxy = new WebProxy("http://proxy:8080");
request.Proxy = webProxy;
string parameters = "";
parameters = "response_type=token";
parameters += "&grant_type=client_credentials";
parameters += "&client_id=sb-c4e-eds-clone-showcase!b1900%7cc4e-eds-test!b462";// + Convert.ToBase64String(Encoding.Default.GetBytes(ClientId));// HttpUtility.UrlEncode(ClientId);
parameters += "&client_secret=2hxc7EmQyArDoO%2f7dSgJJXnPgpg%3d";// + Convert.ToBase64String(Encoding.Default.GetBytes(ClientSecret));// HttpUtility.UrlEncode(ClientSecret);
byte[] byteArray = Encoding.ASCII.GetBytes(parameters);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusCode);
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
JObject data = JObject.Parse(responseFromServer);
authentication = new Authentication();
authentication.AccessToken = data["access_token"].ToString();
authentication.TokenType = data["token_type"].ToString();
authentication.ExpiresIn = data["expires_in"].ToString();
authentication.Scope = data["scope"].ToString();
authentication.Ext_Attr_Enhancer = data["ext_attr"]["enhancer"].ToString();
authentication.JTI = data["jti"].ToString();
DateTime dtExpire = DateTime.Now;
dtExpire = dtExpire.AddSeconds(int.Parse(authentication.ExpiresIn));
authentication.ExpiryDate = dtExpire;
return authentication;
}
public static void updateReadings(
string BearerToken,
string MeterReadingsURL,
string xmlBody)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MeterReadingsURL);
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
request.Headers.Add("Authorization", "Bearer " + BearerToken);
byte[] bytes = Encoding.UTF8.GetBytes(xmlBody);
request.ContentLength = bytes.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusCode);
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Console.WriteLine(responseFromServer);
}
public static string getReadingBodyString(
string MeterUUID,
string TypeName,
List<Reading> lstIntervalReadings)
{
string strReadings = "";
//strReadings += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
strReadings += "<msg:RequestMessage xmlns:msg=\"http://iec.ch/TC57/2011/schema/message\">\n";
strReadings += "\t<msg:Header>\n";
strReadings += "\t\t<msg:Verb>create</msg:Verb>\n";
strReadings += "\t\t<msg:Noun>MeterReadings</msg:Noun>\n";
strReadings += "\t</msg:Header>\n";
strReadings += "\t<msg:Payload>\n";
strReadings += "\t\t<m:MeterReadings xmlns:m=\"http://iec.ch/TC57/CIM-c4e#\">\n";
strReadings += "\t\t\t<m:MeterReading>\n";
strReadings += "\t\t\t\t<m:IntervalBlocks>\n";
foreach (Reading intervalReading in lstIntervalReadings)
{
strReadings += "\t\t\t\t\t<m:IntervalReadings>\n";
strReadings += "\t\t\t\t\t\t<m:timeStamp>" + TimeZone.CurrentTimeZone.ToUniversalTime(intervalReading.Date).ToString("o") + "</m:timeStamp>\n";
strReadings += "\t\t\t\t\t\t<m:value>" + Math.Round(intervalReading.Value, 3) + "</m:value>\n";
strReadings += "\t\t\t\t\t</m:IntervalReadings>\n";
}
strReadings += "\t\t\t\t\t<m:ReadingType ref=\"" + TypeName + "." + MeterUUID + "\"/>\n";
strReadings += "\t\t\t\t</m:IntervalBlocks>\n";
strReadings += "\t\t\t\t<m:Meter>\n";
strReadings += "\t\t\t\t\t<m:mRID>" + MeterUUID + "</m:mRID>\n";
strReadings += "\t\t\t\t</m:Meter>\n";
strReadings += "\t\t\t</m:MeterReading>\n";
strReadings += "\t\t</m:MeterReadings>\n";
strReadings += "\t</msg:Payload>\n";
strReadings += "</msg:RequestMessage>";
strReadings = strReadings.Replace("\t", "");
strReadings = strReadings.Replace("\n", "");
return strReadings;
}
}
}
public void GetAuth()
{
string oAuthUrl = "https://c4e-showcase-account.authentication.sap.hana.ondemand.com/oauth/token";
string clientId = "sb-c4e-eds-clone-showcase!b1900|c4e-eds-test!b462";
string clientSecret = "2hxc7EmQyArDoO/7dSgJJXnPgpg=";
try
{
Authentication authentication = APICalls.AuthenticateAPI(oAuthUrl, clientId, clientSecret);
}
catch(Exception ex)
{
Debug.Log(ex.Message);
}
}
此致,Ben