Advertisement
Vinlock

Elance Oauth2 Auth

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