搜尋
首頁微信小程式微信開發C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一

前言

     微信網頁授權共分為兩種方式:snsapi_base、snsapi_userinfo。 snsapi_base需關注公眾號,取得使用者資訊時不彈出使用者授權介面。 snsapi_userinfo是在使用者未追蹤公眾號的情況下瀏覽頁面,會先彈出一個使用者授權介面,使用者授權後才能拿到使用者資訊。這篇我們先看snsapi_base實作。

 

踩坑注意     

  • 剛開始我調試時snsapi_base方式,點擊頁面地址時提示地址連結不可用,網頁授權需要先在公眾號裡設定網頁基本訊息,也就是設定網域名稱。

  • 區分清楚全域access_token與網頁授權access_token

C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一

C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一

uth  snsapi_base已經拿到使用者的基本訊息,對於一般諮詢類的開發也就夠用了。

看思路

1、透過回掉地址先拿到code。

2、透過code拿到openid。 C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一

3、透過appid和appsecret拿到全域access_token(這點區別於snsapi_userinfo)。

4、透過全域access_token和openid拿到使用者資訊。

 

看代碼

     到這裡也就簡單了,主要是get方式調微信接口,同時準備好你的get和post請求方式,返回參數用json轉化成實體類。

public ActionResult OAuthSnsApiBase() {      
string code = Request.QueryString["code"];      
try     
{           
if (!string.IsNullOrEmpty(code))           
{                   
OAuthToken oauthToken = HttpUtility.Get<OAuthToken>
(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appID, appsecret, code));
                    
string accesstoken = string.Empty;                    
AccessToken token = HttpUtility.Get<AccessToken>(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
appID,appsecret));                    
if (token != null && !string.IsNullOrEmpty(token.access_token))                    
{                        
accesstoken = token.access_token;                    
}                                        
if (oauthToken != null && !string.IsNullOrEmpty(oauthToken.openid))                    
{                                                
OAuthUserInfo userInfo = HttpUtility.Get<OAuthUserInfo>(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", 
accesstoken, oauthToken.openid));                        
if (userInfo != null)                        
{                                                                     
ViewData["headImage"] = userInfo.headimgurl;                            
ViewData["openid"] = userInfo.openid;                            
ViewData["nickName"] = userInfo.nickname;                            
if (userInfo.sex == 0)                            
{                                
ViewData["sex"] = "未知";                            
}                            
else if (userInfo.sex == 1)                            
{                                
ViewData["sex"] = "男";                            
}                            
else                            
{                                
ViewData["sex"] = "女";                            
}                            
ViewData["province"] = userInfo.province;                            
ViewData["city"] = userInfo.city;                        
}                        
else                        
{                                                
}                    
}                    
else                    
{                                           
}                                  
}                
else                
{                    
return Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=123456
#wechat_redirect", appID,"http://"+Request.Url.Host + Url.Action("OAuthSnsApiBase")));                
}            
}            
catch (Exception ex)            
{                         
ViewData["errmsg"] = ex.Message;            
}                        
return View();        
}
 public class OAuthToken    
 {        
 public string access_token { get; set; }        
 public int expires_in { get; set; }        
 public string refresh_token { get; set; }        
 public string openid { get; set; }        
 public string scope { get; set; }    
 } 
 public class AccessToken    
 {        
 public string access_token { get; set; }        
 public int expires_in { get; set; }    
 }public class OAuthUserInfo    
 {        
 public string openid { get; set; }        
 public string nickname { get; set; }        
 public int sex { get; set; }       
  public string province { get; set; }        
  public string city { get; set; }        
  public string country { get; set; }        
  public string headimgurl { get; set; }        
  public string privilege { get; set; }        
  public string unionid { get; set; }            
  }

總結

     這裡取得使用者資訊的介面是

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID。和網頁授權二獲取使用者介面是不一樣的。

C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一

程式碼全部上傳到github裡:https://github.com/garfieldzf8/WeChat

C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資料一帶參考-user-info.html

 

以上就是C#微信公眾號開發--網頁授權(oauth2.0)取得使用者基本資訊一的內容,更多相關內容請關注PHP中文網(www.php.cn) !

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具