简体   繁体   English

在PHP中卷曲以获取access_token OAuth2返回错误:“ invalid_client”

[英]curl in PHP to get access_token OAuth2 return error: “invalid_client”

I got a problem when getting access_token from OAuth2 Server. 从OAuth2服务器获取access_token时出现问题。 I'm using curl in PHP to get the access_token, but it failed and return: 我在PHP中使用curl来获取access_token,但失败并返回:

error: "invalid_client",
error_description: "Client authentication failed."

The strange is, if I use the curl from command prompt: 奇怪的是,如果我在命令提示符下使用curl:

curl -d "client_id=f3d259ddd3ed8ff3843839b&client_secret=4c7f6f8fa93d59c45502c0ae8c4a95b&redirect_uri=http://application.dev/oauth/handle&grant_type=authorization_code&code=rZCQQBXVSQqKi2IRro1gYkSsRhyUcLsNODACjwPw" http://oauth-server.dev/oauth/access_token

it success and return the access_token: 成功并返回access_token:

"access_token":"fI7APDRZygrsF1BiegAQCS1yUT8vnm1LgD5bIu2U",
"token_type":"Bearer",
"expires_in":3600

I'm using Laravel 5.2 and here's my code to handle from OAuth Server to get access_token: 我正在使用Laravel 5.2,这是我的代码,可以从OAuth Server处理以获取access_token:

public function getOAuthHandle(Request $request){
    $url = 'http://oauth-server.dev/oauth/access_token';
    $code = $request->code;
    $client_id = 'f3d259ddd3ed8ff3843839b';
    $client_secret = '4c7f6f8fa93d59c45502c0ae8c4a95b';
    $redirect_uri = 'http://application.dev/oauth/handle';

    $clienttoken_post = array(
                "code" => $code,
                "client_id" => $client_id,
                "client_secret" => $client_secret,
                "redirect_uri" => $redirect_uri,
                "grant_type" => "authorization_code"
            );

            $curl = curl_init($url);

            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
            curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $json_response = curl_exec($curl);
            curl_close($curl);

            return $json_response;
}

I have tried to another script from internet and stackoverflow and restart Apache and PHP, but it still doesn't work. 我曾尝试从Internet和stackoverflow中获取另一个脚本,然后重新启动Apache和PHP,但仍然无法正常工作。

Is there any way to solve this problem? 有什么办法解决这个问题? What should I check? 我应该检查什么?

Thank you for your help and answer. 感谢您的帮助和答复。

Try to send the request on curl headers instead. 尝试改为在curl标头上发送请求。

    $curl = \curl_init();

    \curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 600,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER => $clienttoken_post,
        CURLINFO_HEADER_OUT => true,
    ));

    $response = \curl_exec($curl);

Either sent the grant_type on headers and then client_secret and client_id as post, or sent them on headers try it out. 要么在标头上发送grant_type ,然后将client_secretclient_id作为帖子发送,要么在标头上发送它们,尝试一下。 And btw. 顺便说一句。 $request->code should be checked before passing to curl request. 在传递给curl请求之前,应检查$request->code And all the url, client_secret, client_id MUST be with in .env and get them through config/services . 并且所有url, client_secret, client_id必须在.env ,并通过config/services获得它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:Google plus Oauth 2.0 - 获取OAuth2访问令牌时出错,消息:'invalid_client' - PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client' 未捕获的 Google_Auth_Exception:获取 OAuth2 访问令牌时出错,消息:'invalid_client: Unauthorized' - Uncaught Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_client: Unauthorized' fecing'Google_Auth_Exception'wror获取OAuth2访问令牌,消息:'invalid_client''错误 - fecing 'Google_Auth_Exception' wror fetching OAuth2 access token, message: 'invalid_client'' error OAuth2返回invalid_client错误 - OAuth2 returns invalid_client error 使用PHP cURL在/ oauth2 / access_token上发布数据并以jSON形式获取数据 - Using PHP cURL to POST data on /oauth2/access_token and GET data as jSON 致命错误:未捕获的异常“ Google_Auth_Exception”,消息为“获取OAuth2访问令牌时出错,消息:'invalid_client' - Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' 使用PHP cURL获取access_token时,Google OAuth2.0返回“ invalid_request” - Google OAuth2.0 returns 'invalid_request' when getting access_token with PHP cURL OAuth2:错误401在PHP请求中使用access_token(Discord API)未经授权 - OAuth2: Error 401 Unauthorized at PHP request with access_token (Discord API) 如何使用Google API PHP客户端获取OAuth2访问令牌? - How to get OAuth2 access token with Google API PHP client? Google Adwords API PHP返回invalid_client错误 - Google Adwords API PHP return invalid_client error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM