使用Guzzle 6创建与Api Oauth 2.0的连接

时间:2017-06-12 13:29:32

标签: php oauth guzzle6

方案

  • 使用Oauth 2.0的远程APi
  • 应用程序无法在模式网站上运行。只有控制台
  • 使用Library Guzzle 2.0(客户端)

在远程API文档

  • HTTPS - >发布https://ops.epo.org/3.2/auth/accesstoken
  • HTTP标头授权 - >基本eTNBT0c4NnF3cWpVMFFVNjlWWUdUSjRKR3hVTjhFVkc6cnJYZHI1V0E3eDl 0dWRtUA ==:
  • Contet-Type:application / x-www-form-urlencoded
  • POST请求有效负载
  • grant_type = client_credentials

使用API​​测试的一个网址:

http://ops.epo.org/3.2/rest-services/published-data/publication/docdb/EP1000000/biblio

代码

use GuzzleHttp\Client;

private function connectEPO 
{
    $base_uri           = 'https://ops.epo.org/3.2/';
    $urlAccessToken'    = 'https://ops.epo.org/3.2/auth/accesstoken', 
    $consumerKey        = 'mysecretkey';
    $consumerSecretKey  = 'myconsumersecretkey';

    $client = new Client([
        'base_uri'      => 'https://ops.epo.org/3.2/'
    ]);
    $response = $client->request('POST', 'auth/accesstoken', [
        'auth' => [$consumerKey,$consumerSecretKey]
    ]);
    dd($response); 
}

错误

[GuzzleHttp\Exception\ClientException]                                                                   
  Client error: `POST https://ops.epo.org/3.2/auth/accesstoken` resulted in a `400 Bad Request` response:  
  <error><code>400</code><message>Required param : grant_type</message>                                    
                        </error>                                                      

也可以尝试

$client = new Client([
            'base_uri'      => 'https://ops.epo.org/3.2/'
        ]);
        $request = $client->post('auth/accesstoken', null, array(
            'consumerKey'               => $consumerKey',
            'consumerSecretKey'         => $consumerSecretKey,
            'grant_type'                => 'client_credentials'
        ));

        dd($request);

错误

  [GuzzleHttp\Exception\ClientException]                                                                    
  Client error: `POST https://ops.epo.org/3.2/auth/accesstoken` resulted in a `401 Unauthorized` response:  
  <error><code>401</code><message>Client identifier is required</message>                                   
                        </error>   

尝试几种选择,但我理解或看不到Guzzle手册的正确方法。

0 个答案:

没有答案