Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

Unity3D 网络通信_HTTP协议、处理Json格式返回值、请求加Oauth

post


get


登录

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security.Authentication;
using LitJson;
using System;
using System.Globalization; 
using System.Security.Cryptography;
using System.Web;

public static string url = "http://localhost:8080/film/";

public void Login(string username , string password)
	{	
		string path = "/film/api/tqh/v2/member/login";
		StartCoroutine(PostLogin(url+"api/tqh/v2/member/login?",path,username,password));		
	}
IEnumerator PostLogin(string url,string path,string username , string password)
	{
		string input = "{\"email\":\""+username+"\",\"password\":\""+	ToMd5(password)+"\"}";
		WWWForm form = new WWWForm();		
		Hashtable headers = form.headers;
		byte[] rawData = Encoding.UTF8.GetBytes(input);
		headers["Content-Type"] = "application/json";
		headers["Accept"] = "application/json";

		DateTime date = DateTime.Now;
		string time =  date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo);
		headers["Date"] = time;
		url = hmac(url,path,time);

		WWW www = new WWW(url, rawData, headers);
		yield return www;		
		if (www.error != null)
		{
			Debug.Log("error is login:"+ www.error  + "  " +input );	
			camara.GetComponent<loginGUI>().loginfail(status_fail);
		} else
		{
			Debug.Log("request ok login: " + www.text);

			JsonData jd = JsonMapper.ToObject(www.text); 

			string memberId = jd["memberId"].ToString();
			string bonusPoint = jd["bonusPoint"].ToString();
			string nickName = jd["nickName"].ToString();
			camara.GetComponent<loginGUI>().loginsuccess(memberId,nickName,bonusPoint,status_success);
		}
	}

oauth使用在headers中加入Date,date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo)对日期的格式化

string time =  date.ToString("ddd, yyyy-mm-dd HH':'mm':'ss 'UTC'",DateTimeFormatInfo.InvariantInfo);

headers["Date"] = time;

www.text为后台返回的String字符串,是Json格式,但是不可以直接用key-value去取值,我们需要去转换使用LitJson,一个第三方插件


 
 

JsonData jd = JsonMapper.ToObject(www.text); 

string memberId = jd["memberId"].ToString();
string bonusPoint = jd["bonusPoint"].ToString();
string nickName = jd["nickName"].ToString();

在unity\项目\Assets\Plugins目录下引用LitJson,下载LitJson


之后就可以用key-value去取值了   

oauth的加入

public static  string API_KEY = "bc543dc89b***********";
public  static string SECURE_KEY = "32df2bc3520d53a1************";

	public string hmac(string url ,string path ,string time){
		StringBuilder stringToSign = new StringBuilder();
		stringToSign.Append("POST").Append("\n");
		stringToSign.Append(time).Append("\n");
		stringToSign.Append(path).Append("\n");	

		var signature = string.Empty;
		using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(SECURE_KEY)))
		{
			var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString()));
			signature = Convert.ToBase64String(hash);
		}

		StringBuilder s = new StringBuilder();
		s.Append(url);
		s.Append("&apiKey=");
		s.Append(HttpUtility.UrlEncode(API_KEY, Encoding.UTF8));
		s.Append("&signature=");
		s.Append(HttpUtility.UrlEncode(HttpUtility.UrlEncode(signature, Encoding.UTF8), Encoding.UTF8));

		return s.ToString();
	}


StringBuilder stringToSign = new StringBuilder();
stringToSign.Append("POST").Append("\n");
stringToSign.Append(time).Append("\n");
stringToSign.Append(path).Append("\n");

 

需要通过 httpMethod、time、和path(格式为"/film/api/tqh/v2/public/getAllHistoryRank")来获取HMACSHA512加密后的签名

var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(SECURE_KEY))

对API_KEY和签名进行编码,签名编码两次是因为,在服务器被解码了两次一次jetty、一次后台

StringBuilder s = new StringBuilder();s.Append(url);s.Append("&apiKey=");s.Append(HttpUtility.UrlEncode(API_KEY, Encoding.UTF8));s.Append("&signature=");s.Append(HttpUtility.UrlEncode(HttpUtility.UrlEncode(signature, Encoding.UTF8), Encoding.UTF8));

HttpUtility使用需要引入的 using System.Web; 下载HttpUtility

 
 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要配置 GitLab 的 omniauth_providers 与你的 OAuth2 服务对接,你需要进行以下步骤: 1. 打开 `config/initializers/devise.rb` 文件。 2. 在 `config.omniauth` 部分,添一个新的 omniauth 提供程序配置。根据你的需求,可以选择使用 `:oauth2_generic` 或者 `:generic_oauth` 提供程序。以下是一个使用 `:oauth2_generic` 的示例配置: ```ruby config.omniauth :oauth2_generic, name: 'myam', strategy_class: OmniAuth::Strategies::OAuth2Generic, client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET', client_options: { site: 'https://myam.com', authorize_url: 'https://myam.com/oauth/authorize', token_url: 'https://myam.com/oauth/token' }, user_response_structure: { userApiKey: 'credentials.token', accountName: 'info.account_name', userId: 'uid' } ``` 在上面的示例中,我们使用了 `:oauth2_generic` 提供程序,并提供了必要的配置参数。确保将 `YOUR_CLIENT_ID` 和 `YOUR_CLIENT_SECRET` 替换为你在 OAuth2 服务注册应用时获得的实际值。 3. 根据你的需求,调整 `user_response_structure` 部分的配置以匹配你的 OAuth2 服务返回的用户信息结构。在上面的示例中,我们将 `credentials.token` 映射到 `userApiKey`,将 `info.account_name` 映射到 `accountName`,将 `uid` 映射到 `userId`。 4. 保存文件并重启 GitLab。 通过以上步骤,你应该能够成功配置 GitLab 的 omniauth_providers 与你的 OAuth2 服务对接,并使用你的 OAuth2 服务返回的用户信息结构。确保根据你的实际情况进行适当的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值