How uin-app obtains WeChat nickname and avatar blog

The WeChat login function is used in many applications, which can facilitate users to quickly complete registration, login and other operations. This article will introduce how to obtain WeChat user's nickname and avatar information through uin-app.

Step 1: Prepare the development environment

First, you need to download and install the QQ Lite Development Tool (uin-app) for development. It can be downloaded from the official website: Tencent Open Platform beta | Openness makes dreams come true

Step 2: Create an application and configure it

After logging in to the OpenUP open platform, enter the application management page, create a new application, and select Web or H5 as the application type.

Enter the application details page to obtain parameters such as application ID (APPID) and APPKEY (Secret Key).

Enable the "Pull user information" permission in the interface permission configuration.

Step 3: Call API to get information

API interface:

https://graph.qq.com/user/get_user_info?access_token=YOUR_ACCESS_TOKEN&oauth_consumer_key=YOUR_APP_ID&openid=YOUR_OPENID

illustrate:

  • access_token: the access token issued by the QQ login server.
  • oauth_consumer_key: APPID of the application.
  • openid: The openid passed by the user when authorizing.

Application developers can call the above API interface on the back-end server according to actual needs, obtain information such as WeChat user nicknames and avatars, and return them to the front-end of the application for display.

The sample code is as follows:

import requests

def get_user_info(access_token, openid):
    url = f'https://graph.qq.com/user/get_user_info?access_token={access_token}&oauth_consumer_key={app_id}&openid={openid}'
    response = requests.get(url)
    
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Failed to get user info: {response.text}")

Step 4: Display user information

After obtaining information such as nicknames and avatars of WeChat users, users' personal information can be displayed according to requirements. For example, user avatars and nicknames can be displayed at the top of the page, and operations such as a personal center can be provided.

Sample code:

<div class='user-info'>
    <img src={
   
   {avatar}} class='user-avatar'/>
    <span class='user-nickname'>{
   
   {nickname}}</span>
</div>

The above is the process and implementation method of using uin-app to obtain WeChat user nickname and avatar. If you also need to use QQ to log in, just follow the instructions in the document to develop.

Precautions:

  • When calling the API, you need to pass access_token and openid parameters. These parameters need to be passed from the front end to the back end and cannot be exposed to users.
  • Pay attention to the requirements of the privacy policy to ensure that the user's personal information is not disclosed.

Attach the complete code: https://github.com/your-username/your-project-name

Hope to help you, thank you!

Guess you like

Origin blog.csdn.net/qq_70703397/article/details/131084446