Foros del Web » Programando para Internet » PHP »

Autentificacion con OAuth2 - PHP

Estas en el tema de Autentificacion con OAuth2 - PHP en el foro de PHP en Foros del Web. Hola gente del foro, tengo que conectarme a una API y enviarle datos en formato JSON mi app. esta desarrollada con php y me especifican ...
  #1 (permalink)  
Antiguo 08/04/2015, 11:43
 
Fecha de Ingreso: octubre-2014
Ubicación: Buenos Aires
Mensajes: 278
Antigüedad: 9 años, 6 meses
Puntos: 12
Exclamación Autentificacion con OAuth2 - PHP

Hola gente del foro, tengo que conectarme a una API y enviarle datos en formato JSON mi app. esta desarrollada con php y me especifican que la autentificación tiene que ser por OAuth2 alguien tiene idea de como se hace o tienen algún ejemplo.?

Desde ya muchas gracias.

Saludos.
__________________
http://www.sp-vision.net
  #2 (permalink)  
Antiguo 08/04/2015, 11:49
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Autentificacion con OAuth2 - PHP

Tienes que crear un cliente que utilice el protocolo o usar uno https://github.com/fkooman/php-oauth-client
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 08/04/2015, 14:11
 
Fecha de Ingreso: octubre-2014
Ubicación: Buenos Aires
Mensajes: 278
Antigüedad: 9 años, 6 meses
Puntos: 12
Respuesta: Autentificacion con OAuth2 - PHP

Gracias hhs, revisare lo que me pasaste y si tengo alguna duda la posteare.

Saludos.
__________________
http://www.sp-vision.net
  #4 (permalink)  
Antiguo 09/04/2015, 06:44
 
Fecha de Ingreso: octubre-2014
Ubicación: Buenos Aires
Mensajes: 278
Antigüedad: 9 años, 6 meses
Puntos: 12
Respuesta: Autentificacion con OAuth2 - PHP

Investigando por la web encentré esta clase.

elance-auth-lib.php

Código PHP:
Ver original
  1. <?php
  2. class ElanceAuthentication {
  3.     public $CurlHeaders;
  4.     public $ResponseCode;
  5.  
  6.     private $_AuthorizeUrl = "https://api.elance.com/api2/oauth/authorize";
  7.     private $_AccessTokenUrl = "https://api.elance.com/api2/oauth/token";
  8.  
  9.     public function __construct() {
  10.         $this->CurlHeaders = array();
  11.         $this->ResponseCode = 0;
  12.     }
  13.  
  14.     public function RequestAccessCode ($client_id, $redirect_url) {
  15.         return($this->_AuthorizeUrl . "?client_id=" . $client_id . "&response_type=code&redirect_uri=" . $redirect_url);
  16.     }
  17.  
  18.     // Convert an authorization code from an Elance callback into an access token.
  19.     public function GetAccessToken($client_id, $client_secret, $auth_code) {
  20.         // Init cUrl.
  21.         $r = $this->InitCurl($this->_AccessTokenUrl);
  22.  
  23.         // Add client ID and client secret to the headers.
  24.         curl_setopt($r, CURLOPT_HTTPHEADER, array (
  25.             "Authorization: Basic " . base64_encode($client_id . ":" . $client_secret),
  26.         ));        
  27.  
  28.         // Assemble POST parameters for the request.
  29.         $post_fields = "code=" . urlencode($auth_code) . "&grant_type=authorization_code";
  30.  
  31.         // Obtain and return the access token from the response.
  32.         curl_setopt($r, CURLOPT_POST, true);
  33.         curl_setopt($r, CURLOPT_POSTFIELDS, $post_fields);
  34.  
  35.         $response = curl_exec($r);
  36.         if ($response == false) {
  37.             die("curl_exec() failed. Error: " . curl_error($r));
  38.         }
  39.  
  40.         //Parse JSON return object.
  41.         return json_decode($response);
  42.     }
  43.  
  44.     private function InitCurl($url) {
  45.         $r = null;
  46.  
  47.         if (($r = @curl_init($url)) == false) {
  48.             header("HTTP/1.1 500", true, 500);
  49.             die("Cannot initialize cUrl session. Is cUrl enabled for your PHP installation?");
  50.         }
  51.  
  52.         curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
  53.  
  54.         // Decode compressed responses.
  55.         curl_setopt($r, CURLOPT_ENCODING, 1);
  56.  
  57.         // NOTE: If testing locally, add the following lines to use a dummy certificate, and to prevent cUrl from attempting to verify
  58.         // the certificate's authenticity. See http://richardwarrender.com/2007/05/the-secret-to-curl-in-php-on-windows/ for more
  59.         // details on this workaround. If your server has a valid SSL certificate installed, comment out these lines.
  60.         curl_setopt($r, CURLOPT_SSL_VERIFYPEER, false);
  61.         curl_setopt($r, CURLOPT_CAINFO, "C:\wamp\bin\apache\Apache2.2.21\cacert.crt");
  62.  
  63.         // NOTE: For Fiddler2 debugging.
  64.         //curl_setopt($r, CURLOPT_PROXY, '127.0.0.1:8888');
  65.  
  66.         return($r);
  67.     }
  68.  
  69.     // A generic function that executes an Elance API request.
  70.     public function ExecRequest($url, $access_token, $get_params) {
  71.         // Create request string.
  72.         $full_url = http_build_query($url, $get_params);
  73.  
  74.         $r = $this->InitCurl($url);
  75.  
  76.         curl_setopt($r, CURLOPT_HTTPHEADER, array (
  77.             "Authorization: Basic " . base64_encode($access_token)
  78.         ));
  79.  
  80.         $response = curl_exec($r);
  81.         if ($response == false) {
  82.             die("curl_exec() failed. Error: " . curl_error($r));
  83.         }
  84.  
  85.         //Parse JSON return object.
  86.         return json_decode($response);        
  87.     }
  88. }
  89. ?>

user-authentification.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. require_once('elance-auth-lib.php');
  4.  
  5.  
  6. $elance_auth = new ElanceAuthentication();
  7. $url = $elance_auth->RequestAccessCode("4eb1de8bf06b10210e000005", "http://localhost/test/callback.php");
  8.  
  9. header("Location: " . $url);
  10.  
  11. ?>

callback.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. require_once('elance-auth-lib.php');
  4.  
  5. if (!isset($_GET["code"])) {
  6.     die("Require the code parameter to validate!");
  7. }
  8.  
  9. $code = $_GET["code"];
  10. $elance_auth = new ElanceAuthentication();
  11. $json = $elance_auth->GetAccessToken("4eb1de8bf06b10210e000005", "M5IqdtQdZN8cX741OkBniA", $code);
  12.  
  13. //Output code
  14. echo "<p>Access token is " . $json->data->access_token . "<p/>";
  15.  
  16. ?>

Puede servir para conectarme por medio de OAuth2 a una api ? sepan disculpar ya que estoy un poco perdido con este tema.

Desde ya muchas gracias.
__________________
http://www.sp-vision.net
  #5 (permalink)  
Antiguo 09/04/2015, 08:58
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: Autentificacion con OAuth2 - PHP

Necesitas probarlo y leer sobre el protocolo auth2 para que sepas como funciona.
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:22.