Last active
June 24, 2019 09:54
-
-
Save DxPoly/bd4fbb0ed05fdca8e10624e0760bf81d to your computer and use it in GitHub Desktop.
钉钉扫码登录第三方网站
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 用于rails的代码片段,省略了各种引入第三方库的代码 | |
class Dingding | |
def self.get_user_info_by_code code | |
timestamp = (Time.now.to_f * 1000).to_i | |
sign = hmac_sign(APP_SECRET, timestamp) | |
url = "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=APPID}×tamp=#{timestamp}&signature=#{sign}" | |
RestClient.post(url, { tmp_auth_code: code }.to_json, { 'Content-Type' => 'application/json' }) do |response| | |
return JSON.parse(response.body).symbolize_keys | |
end | |
end | |
private | |
# HMACSHA256 签名 | |
def self.hmac_sign(key, data) | |
hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key.to_s, data.to_s) | |
sign = Base64.strict_encode64(hmac) | |
puts sign.inspect | |
CGI::escape(sign) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment