没有不值得去解决的问题,也没有不值得去学习的技术!

Fixing Weibo Micro-Connect Web App Authorization: Why a Second User Cannot Be Created After the First in the Same Browser

Now, within the same browser, you can access the authorization page to log in and consent to authorization using another Weibo account (terryhong123)

1. Obtain the link for the Weibo Web Connect web application authorization (guiding the user to the authorization page to log in and consent to authorization, creating a user for the Weibo Web Connect web application), as shown in Figure 1.

Link for the Weibo Web Connect web application authorization (guiding the user to the authorization page to log in and consent to authorization, creating a user for the Weibo Web Connect web application)
Figure 1

2. Open the URL: https://weibo.com/ , and confirm that the Weibo account is not logged in, as shown in Figure 2.

Open the URL: https://weibo.com/ , and confirm that the Weibo account is not logged in
Figure 2

3. Weibo Web Connect web application authorization (guiding the user to the authorization page to log in and consent to authorization, creating a user for the Weibo Web Connect web application): log in and consent to authorization, as shown in Figure 3.

Weibo Web Connect web application authorization (guiding the user to the authorization page to log in and consent to authorization, creating a user for the Weibo Web Connect web application): log in and consent to authorization
Figure 3

4. Exchange Code for the third-party authorization Access Token, perform the corresponding data operations, create a user for the Weibo Web Connect web application, and obtain the user list for the Weibo Web Connect web application, as shown in Figure 4.

Exchange Code for the third-party authorization Access Token, perform the corresponding data operations, create a user for the Weibo Web Connect web application, and obtain the user list for the Weibo Web Connect web application
Figure 4

5. Open the URL: https://weibo.com/ , and confirm that the Weibo account (永夜烟) is logged in, as shown in Figure 5.

Open the URL: https://weibo.com/ , and confirm that the Weibo account (永夜烟) is logged in
Figure 5

6. At this point, within the same browser, we need to access the authorization page to log in and consent to authorization using another Weibo account (terryhong123) to create a user for the Weibo Web Connect web application. The creation ultimately fails. Analysis: The first Weibo account (永夜烟) is already in a logged-in state in the same browser, so the login page is automatically skipped to create a user for the Weibo Web Connect web application. However, because a unique index is set on user_id, the creation fails, as shown in Figure 6.
If the user reopens the authorization page to authorize during the valid authorization period (if the user has a Weibo login status at this time, this page will flash by).

At this point, within the same browser, we need to access the authorization page to log in and consent to authorization using another Weibo account (18328600160) to create a user for the Weibo Web Connect web application. The creation ultimately fails. Analysis: The first Weibo account (永夜烟) is already in a logged-in state in the same browser, so the login page is automatically skipped to create a user for the Weibo Web Connect web application. However, because a unique index is set on user_id, the creation fails.
Figure 6

7. The current requirement is to be able to repeatedly create multiple users for the Weibo Web Connect web application within the same browser (when creating the second user, it is necessary to enter the authorization page again to log in and consent to authorization). Reviewing the OAuth2.0 documentation, I found that it supports a forced login feature, as shown in Figure 7.
Forced login
The authorization page will read the current user’s Weibo login state by default. If you want the user to log in again, pass the parameter: forcelogin=true when calling the authorize interface. Leaving this parameter blank by default is equivalent to forcelogin=false.

The current requirement is to be able to repeatedly create multiple users for the Weibo Web Connect web application within the same browser (when creating the second user, it is necessary to enter the authorization page again to log in and consent to authorization). Reviewing the OAuth2.0 documentation, I found that it supports a forced login feature.
Figure 7

8. Edit \frontend\controllers\WeiboOauth2Controller.php’s public function actionAuthorize(), and pass the parameter: forcelogin=true when calling the authorize interface.

PHP

        /* 浏览器 302 跳转:引导用户进入授权页面登录同意授权,获取 code */
        Yii::$app->response->redirect(Yii::$app->params['weiboAuth']['hostInfo'] . Yii::$app->params['weiboAuth']['baseUrl'] . '/authorize?response_type=code&forcelogin=true&client_id=' . Yii::$app->params['weiboAuth']['weiboConnectWebApp']['appId'] . '&redirect_uri=' . $redirectUri);

9. Now, within the same browser, you can access the authorization page to log in and consent to authorization using another Weibo account (terryhong123), as shown in Figure 8.

Now, within the same browser, you can access the authorization page to log in and consent to authorization using another Weibo account (terryhong123)
Figure 8

10. Exchange Code for the third-party authorization Access Token, perform the corresponding data operations, create a user for the Weibo Web Connect web application, and obtain the user list for the Weibo Web Connect web application, as shown in Figure 9.

Exchange Code for the third-party authorization Access Token, perform the corresponding data operations, create a user for the Weibo Web Connect web application, and obtain the user list for the Weibo Web Connect web application.
Figure 9

11. Open the URL: https://weibo.com/ , and confirm that the Weibo account (永夜烟) has logged out, as shown in Figure 10.

Open the URL: https://weibo.com/ , and confirm that the Weibo account (永夜烟) has logged out
Figure 10

12. Refresh the URL: https://weibo.com/ , and confirm that the Weibo account (terryhong123) is logged in, as shown in Figure 11.

Refresh the URL: https://weibo.com/ , and confirm that the Weibo account (terryhong123) is logged in
Figure 11

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系