微信授權登入

2021-08-07 02:46:12 字數 2244 閱讀 6546

// 使用者同意授權url(對urlencode之後)

private static string redirect_url = "";

// 使用者同意授權url

private static string authorization_url = "";

/*** 建立授權url

* * @param state

* @return

* @throws exception

*/public mapcreateauthorizationurl(string commontoken,

string state) throws exception

// 轉成json

jsonobject json = jsonobject.parseobject(result);

// 儲存state

mapdata = new hashmap();

data.put("state", state);

data.put("expiredtime", (system.currenttimemillis() + 300000)); // state過期時間

// 儲存state

// 注:由於本人沒有接觸過redis,再加上專案緊,來不及現學

// 所以我這一步是將state存放在了資料庫裡,

// 個人建議最好存放在快取中(當然主要還是要根據專案來)

wechatstatedao.add(data);

// 返回值

mapresultmap = new hashmap();

resultmap.put("state", state);

resultmap.put("shorturl", json.getstring("short_url"));

return resultmap;

}

/**

* 獲取授權認證(for user)

* code 用於換取特殊(access_token,該token可以獲取使用者相關資訊)

* state 用於驗證

// 獲取過期時間

long expiredtime = (long) entity.get("expiredtime");

if (expiredtime < system.currenttimemillis())

// 用code換取access_token相關資訊

jsonobject tokenjson = authenticationservice.getaccesstoken(code);

// access_token

string accesstoken = (string) tokenjson.get("access_token");

// 使用者唯一標識

string openid = (string) tokenjson.get("openid");

// 更新state

entity.put("openid", openid);

authenticationservice.updatestate(entity);

// 根據openid獲取使用者資訊,判斷使用者是否註冊過

userinfo userinfo = userinfoservice.getbyopenid(openid);

// 如果使用者未註冊過,則新增

if (userinfo == null)

response.sendredirect("");

} catch (exception e)

}

三、頁面輪詢

/**

* 校驗state(頁面輪詢呼叫 for user)

return jacksonutil.tobizjson("0", "驗證通過", data);

} catch (exception e)

}

稍微歸納之後的**:

微信授權登入

首先說一下我寫這個的初衷 注意了 使用開放平台,需要備案的網域名稱才可以。且開放平台需要認證 使用公眾平台,那就更不要說了,更需要認證。開發反而不難了,因為那個對著api文件花些時間就可以了,就不貼了,那個沒啥意思,上面的資訊值金子 需要認證,需要認證,需要認證 開放平台api 公眾平台api 當然...

iOS 微信授權登入

通過access token進行介面呼叫,獲取使用者基本資料資源或幫助使用者實現基本操作。3 準備工作 3.1 更改plist檔案如下 這樣就可以允許http傳輸了 那麼 如下 ibaction sendauthrequest 4.1 引數說明 4.3 返回結果說明 通過code獲取access t...

微信網頁授權登入

筆記小結 1.公眾平台以access token為介面呼叫憑據,所有介面呼叫前都要先獲取access token 2.網頁授權兩種方式 oauth機制 2.1 以snsapi base為snsapi的靜默授權,只能獲取到使用者的openid,2.2 以snsapi userinfo為snsapi的授...