Home WeChat Applet Mini Program Development WeChat applet obtains user mobile phone number code sharing

WeChat applet obtains user mobile phone number code sharing

May 19, 2018 pm 03:39 PM
code Applets Phone number

This article mainly introduces in detail how the WeChat applet obtains the user's mobile phone number. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

I am currently working on a WeChat applet, which requires obtaining the user’s mobile phone number. The specific steps are as follows:

Flow chart:


1. First, the client calls wx.login, and the callback data contains jscode, which is used to obtain openid (user unique identification) and sessionkey (session key).

2. After getting the jscode, send it to the server. The server uses it to interact with the WeChat server to obtain the openid and sessionkey. The specific acquisition method is as follows:

(1) You need to write a HttpUrlConnection tool class:

public class MyHttpUrlConnection { 
 private final int mTimeout = 10000; // 超时时间 
 /** 
 * get访问 
 */ 
 public String[] requestJson(String url) { 
 return request(url); 
 } 
 private String[] request(String connurl) { 
 String[] resultStr = new String[]{"", ""}; 
 StringBuilder resultData = new StringBuilder(""); 
 HttpURLConnection conn = null; 
 try { 
  URL url = new URL(connurl); 
  conn = (HttpURLConnection) url.openConnection(); 
  conn.setRequestMethod("GET"); 
  conn.setUseCaches(false); 
  conn.setConnectTimeout(mTimeout); 
  conn.connect(); 
  int resultCode = conn.getResponseCode(); 
  InputStreamReader in; 
  if (resultCode == 200) { 
  in = new InputStreamReader(conn.getInputStream()); 
  BufferedReader buffer = new BufferedReader(in); 
  String inputLine; 
  while ((inputLine = buffer.readLine()) != null) { 
   resultData.append(inputLine); 
   resultData.append("\n"); 
  } 
  buffer.close(); 
  in.close(); 
  } 
  resultStr[0] = resultData.toString(); 
  resultStr[1] = resultCode + ""; 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } finally { 
  if (conn != null) { 
  conn.disconnect(); 
  } 
 } 
 return resultStr; 
 } 
}
Copy after login

(2) Then establish a connection with the WeChat server through this tool class to obtain the desired data:

 String url = "https://api.weixin.qq.com/sns/jscode2session?appid=""&secret=""&js_code=" 
   + jsCode + "&grant_type=authorization_code"; 
 String res[] = connection.requestJson(url); 
 System.out.println(res[0]); 
 JSONObject object = JSON.parseObject(res[0]); 
 String openId = object.getString("openid"); 
 String session_key = object.getString("session_key");
Copy after login

The appid and secret can be queried in your own developer account, and the js_code is sent by the client, so that the sessionkey can be obtained from the returned data.

3. After server A gets the sessionkey, it generates a random number we call 3rdsession, with 3rdSessionId as the key and sessionkey + openid as the value and cached in redis or memcached; because the WeChat team does not recommend directly storing the sessionkey in Transmitted over the network, the developer generates a unique key and associates it with the session key. Its functions are: (1) Return the 3rdSessionId to the client to maintain the mini program login state.

(2). Find user sessionkey and openid through 3rdSessionId.

4. The client gets the 3rdSessionId and caches it in storage.
5. User sensitive data encryptedData can be obtained through wx.getUserIinfo.
6. The client sends encryptedData, 3rdSessionId and offset together to server A
7. Server A obtains session_key from the cache based on 3rdSessionId
8. Use AES to decrypt encryptedData on server A to achieve user sensitivity Data decryption.

There are three parameters needed to decrypt data, namely:

1, encryptedData (ciphertext)
2, iv (vector)
3, aesKey (Key) That is, sessionkey

When decrypting, the above three variables must be Base64 decoded:

byte[] encrypData = UtilEngine.decode(encData); 
byte[] ivData = UtilEngine.decode(iv); 
byte[] sessionKey = UtilEngine.decode(session_key);
Copy after login

Then use the AES decryption method to decrypt:

public static byte[] decrypt(byte[] key, byte[] iv, byte[] encData) 
 throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, 
 InvalidKeyException, BadPaddingException, IllegalBlockSizeException { 
 AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); 
 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
 SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); 
 return cipher.doFinal(encData); 
}
Copy after login

In this way, the user's mobile phone number can be obtained in the returned data.

Related recommendations:

Examples to explain the WeChat applet's function of obtaining a mobile phone number to authorize user login

PHP to obtain a mobile phone number Regular expression writing

PHP obtains the location of mobile phone number through API_PHP tutorial

The above is the detailed content of WeChat applet obtains user mobile phone number code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

How to set up if WeChat does not allow adding by mobile phone number How to set up if WeChat does not allow adding by mobile phone number Mar 12, 2024 pm 05:04 PM

How to set up WeChat to disallow adding via mobile phone number? In WeChat APP, you can disallow adding WeChat via mobile phone number, but most users don’t know how to set it up to not allow WeChat to be added via mobile phone number. Next is WeChat brought by the editor to users It is not allowed to add setting method tutorials through mobile phone numbers. Interested users can come and take a look! Tutorial on how to use WeChat. How to set up WeChat when adding a mobile phone number is not allowed. 1. First open the WeChat APP, enter the main page, click [Me] in the lower right corner to enter, and select the [Settings] function; 2. Then enter the setting function page and find [Friend Permissions] ] Service function; 3. Then on the friend permissions interface, slide the button behind [Verification is required when adding me as a friend]; 4. Finally, on the page shown below, slide [Mobile Phone Number]

How to solve win7 driver code 28 How to solve win7 driver code 28 Dec 30, 2023 pm 11:55 PM

Some users encountered errors when installing the device, prompting error code 28. In fact, this is mainly due to the driver. We only need to solve the problem of win7 driver code 28. Let’s take a look at what should be done. Do it. What to do with win7 driver code 28: First, we need to click on the start menu in the lower left corner of the screen. Then, find and click the "Control Panel" option in the pop-up menu. This option is usually located at or near the bottom of the menu. After clicking, the system will automatically open the control panel interface. In the control panel, we can perform various system settings and management operations. This is the first step in the nostalgia cleaning level, I hope it helps. Then we need to proceed and enter the system and

How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? Mar 22, 2024 am 08:11 AM

In the digital age, Xiaohongshu has become a platform for many people to share their lives and discover good things. Over time, we may need to change the mobile phone number on the account. So, how to change Xiaohongshu’s mobile phone number? 1. How to change the mobile phone number of Xiaohongshu account? 1. Log in to your Xiaohongshu account and enter the “My” page. 2. Click "..." in the upper right corner and select "Settings". 3. On the settings page, find the "Account and Security" option and click to enter. 4. On the Account and Security page, find the "Mobile Phone Number" option and click to enter. 5. At this time, the page will display the current mobile phone number, click "Change Mobile Number". 6. Enter the new mobile phone number, receive the verification code, and fill it out. 7. Set a new password to ensure account security. 8. After completing the above steps,

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

The computer frequently blue screens and the code is different every time The computer frequently blue screens and the code is different every time Jan 06, 2024 pm 10:53 PM

The win10 system is a very excellent high-intelligence system. Its powerful intelligence can bring the best user experience to users. Under normal circumstances, users’ win10 system computers will not have any problems! However, it is inevitable that various faults will occur in excellent computers. Recently, friends have been reporting that their win10 systems have encountered frequent blue screens! Today, the editor will bring you solutions to different codes that cause frequent blue screens in Windows 10 computers. Let’s take a look. Solutions to frequent computer blue screens with different codes each time: causes of various fault codes and solution suggestions 1. Cause of 0×000000116 fault: It should be that the graphics card driver is incompatible. Solution: It is recommended to replace the original manufacturer's driver. 2,

Resolve code 0xc000007b error Resolve code 0xc000007b error Feb 18, 2024 pm 07:34 PM

Termination Code 0xc000007b While using your computer, you sometimes encounter various problems and error codes. Among them, the termination code is the most disturbing, especially the termination code 0xc000007b. This code indicates that an application cannot start properly, causing inconvenience to the user. First, let’s understand the meaning of termination code 0xc000007b. This code is a Windows operating system error code that usually occurs when a 32-bit application tries to run on a 64-bit operating system. It means it should

Detailed explanation of the causes and solutions of 0x0000007f blue screen code Detailed explanation of the causes and solutions of 0x0000007f blue screen code Dec 25, 2023 pm 02:19 PM

Blue screen is a problem we often encounter when using the system. Depending on the error code, there will be many different reasons and solutions. For example, when we encounter the problem of stop: 0x0000007f, it may be a hardware or software error. Let’s follow the editor to find out the solution. 0x000000c5 blue screen code reason: Answer: The memory, CPU, and graphics card are suddenly overclocked, or the software is running incorrectly. Solution 1: 1. Keep pressing F8 to enter when booting, select safe mode, and press Enter to enter. 2. After entering safe mode, press win+r to open the run window, enter cmd, and press Enter. 3. In the command prompt window, enter "chkdsk /f /r", press Enter, and then press the y key. 4.

See all articles