In Yii 2, when processing a RESTful API request, the new response format is supported: text/html, and only the HTML format is supported
1. Open the URL in the browser: http://api.channel-pub-api.localhost/qq/v1/OA uth2/authorize?group_id=015ce30b116ce86058fa6ab4fea4ac63 , which responds to the XML format, because RESTful APIs support both JSON and XML formats. But HTML format is not supported. as shown in Figure 1
2. Open in postman, the value of accept is: application/json; version=0.0, respond to json format, as shown in Figure 2
{
"name": "unprocessable entity",
"message": "Data validation failed: the callback link redirected after the third-party service platform is authorized cannot be empty",
"code": 40004,
"status": 422,
"type": "yii\web\\unprocessableEntityHttpException"
}
3. Now it is necessary to add support for the HTML format, and only support the HTML format, because it is a page, not an interface, and finally jump to the corresponding callback link page, edit \qq\rests\oauth2\authorizeaction.php, set the format attribute, the format attribute specifies the style of the data in data format
<?php
/**
* @Link http://www.yiiframework.com/
* @Copyright Copyright (C) 2008 Yii Software LLC
* @License http://www.yiiframework.com/license/
*/
namespace QQ\RESTS\OAuth2;
use yii;
use yii\base\model;
use yii\web\response;
use yii\base\dynamicmodel;
Use Yii\Web\ServerErrorHttpException;
use yii\web\unprocessableEntityHttpException;
/**
* Authorization of the third-party service platform (steer the user to the authorization page to log in to agree to authorize)
;
* 1. Request parameter list
* (1) redirect_uri: required, the callback link redirected after authorization of the third-party service platform
;
* 2. Input data verification rules
* (1) Required: redirect_uri
* (2) URL: redirect_uri
;
* 3. Operation data
* (1)302 Jump to https://auth.om.qq.com/omoauth2/authorize?response_type=code&client_id=626d0 ce988bf5fddb3d9dd9ce627b2ba&state=state&redirect_uri=http%3a%2f%2fapi.ch annel-pub-api-localhost.chinamcloud.com%2fqq%2fv1%2foauth2%2faccess-token%3fgrou p_id%3d015ce30b116ce86058fa6ab4fea4ac63%26redirect_uri%3dhttp%3a%2f%2fwww.zmt.com
;
* for more details and usage information on authorize action, see the[guide article on rest controllers](guide:rest-controllers).
;
* @author qiang wang <shuijingwanwq@163.com>
* @since 1.0
*/
class authorize action extends action
{
/**
* @var string the scene to be assigned to the new model before it is validated and saved.
*/
public $scenario = model::scenario_default;
/**
* @var string the name of the view action. This property is need to create the URL when the model is successfully created.
*/
public $ViewAction =view;
/**
* Authorizes a new model.
* @return \yii\db\ActiveCordInterface the model newly created
* @Throws ServerErrorHttpException if there is any error when creating the model
*/
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id);
}
$request = yii::$app->request;
$get = $request->get();
$redirect_uri = $request->get(redirect_uri);
yii::$app->response->format = response::format_html;
// temporary verification
$model = dynamicModel::validateData(compact(redirect_uri,[
[[[redirect_uri]#ATFP_CLOSE_Translate_span#,required],
[['redirect_uri'],url],
]);
if ($model->hasErrors()) {
foreach ($model->getfirsErrors() as $message) {
$firsErrors = $message;
break;
}
Throw new UnprocessableEntityHttpException(yii::t(Error, yii::t(Error, yii::t(Error,20004,['firstErrors' => $firstErrors])), 20004);
}
// The entire URL containing the host info, encode the URL string
$redirectUri = urlencode($request->hostinfo . $request->BaseURL ./v1/oauth2/access-token?group_id=. yii::$app->params['groupId'].&redirect_uri=. $redirect_uri);
/* Browser jump: guide the user to the authorization page to log in to agree to authorize, obtain code */
yii::$app->response->redirect(yii::$app->params['qqAuth']['hostInfo']. yii::$app->params['qqAuth']['baseUrl']./authorize?response_type=code&client_id=. yii::$app->params['qqAuth']['tpApp']['clientId'].&state=state&redirect_uri=. $redirectUri);
}
}
4. Open the URL in the browser: http://api.channel-pub-api.localhost/qq/v1/OA uth2/authorize?group_id=015ce30b116ce86058fa6ab4fea4ac63 , in response to the HTML format, as expected, as shown in Figure 3
5. Open in postman, the value of accept is: application/json; version=0.0, respond to HTML format, which is in line with expectations, as shown in Figure 4



