收集的一些资料
。
。
使用百度的OAuth服务进行测试
代码下载:http://yunpan.cn/QtNuCR9I4jDz8。
html代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="OAuthoStudy.Index" %> 2 3 4 5 6 7 819 20 219 10 11 12
后台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 using System.Net; 9 using Newtonsoft.Json;10 11 namespace OAuthoStudy12 {13 public partial class Index : System.Web.UI.Page14 {15 protected void Button1_Click(object sender, EventArgs e)16 {17 var access_token = this.GetAccessToken();18 WebRequest request = WebRequest.Create("https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=" + access_token);19 20 var response = request.GetResponse();21 StreamReader sr = new StreamReader(response.GetResponseStream());22 dynamic data = JsonConvert.DeserializeObject(sr.ReadToEnd());23 24 this.Response.Write("欢迎您:" + data.username);25 }26 27 private string GetAccessToken()28 {29 WebRequest request = WebRequest.Create("https://openapi.baidu.com/oauth/2.0/token?client_id=hdFhQoUiu9GdeqBzPStXf9li&client_secret=NLzxHLzbWn2XGZrQbsfuG3lBYX5tcBHZ&grant_type=authorization_code&code=" + this.TextBox1.Text + "&redirect_uri=oob");30 31 var response = request.GetResponse();32 StreamReader sr = new StreamReader(response.GetResponseStream());33 dynamic data = JsonConvert.DeserializeObject(sr.ReadToEnd());34 35 return data.access_token.ToString();36 }37 }38 }
运行效果
备注
OAuth的主要作用应该是授权,具体还要继续思考。