2011-11-17 29 views
1

我正嘗試創建一個訪問linkedin API的cakePHP應用程序。我在麪包店發現了一個插件,這正是我要找的: http://bakery.cakephp.org/articles/inlet/2011/07/13/linkedin_plugin無法讓CakePHP Linkedin插件正常工作

不幸的是,在安裝插件後,它無法正常工作。運行$ this-> Linkedin-> connect();函數的結果被引導到一個linkedin頁面,上面寫着: 「我們無法找到授權令牌」

我能夠使用PHP的Simple-LinkedIn演示連接,所以我知道我沒有問題與我的系統時間或API密鑰。

經過許多小時的調試和了解Linkedin的OAuth過程之後,我相信我已經縮小了問題的範圍,當插件獲取請求令牌時,它並未從Linkedin接收任何內容,因此它沒有嘗試授權用戶時使用有效令牌。我懷疑令牌請求帖子結構不正確。

有沒有人有這個插件成功?有誰知道CakePHP的另一個Linkedin插件嗎?

+0

是否遵循了獲取OAuth令牌的步驟? http://developer.linkedin.com/documents/getting-oauth-token – Oldskool

回答

0

看起來您在處理CakePHP的LinkedIn OAuth身份驗證時遇到問題,我可以建議Opauth

參見:

Opauth使登錄操作簡單。

獲得access_token後,您可以繼續使用其他LinkedIn PHP庫來調用LinkedIn的其他API調用。

免責聲明:我是Opauth的作者。它看起來像你的情況,使用Opauth可能會爲你節省一些麻煩。如果您有任何問題,我可以幫您解決。

0
Controller file : - LinkedinController.php 

/* Start Controller File code*/ 

<?php 
/** 
* 
* @copyright  Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) 
* @link   http://cakephp.org CakePHP(tm) Project 
* @package  app.Controller 
* @since   CakePHP(tm) v 0.2.9 
* @license  http://www.opensource.org/licenses/mit-license.php MIT License 
*/ 

App::uses('AppController', 'Controller'); 
App::uses('CakeEmail', 'Network/Email'); 
//App::import('Vendor', 'Linkedin',array('file'=>'process'.DS.'process.php')); 
App::import('Vendor', 'Linkedin', array('file' => 'Linkedin/oauth_client.php')); 
/** 
* Static content controller 
* 
* Override this controller by placing a copy in controllers directory of an application 
* 
* @package  app.Controller 
* @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html 
*/ 

class LinkedinController extends AppController { 


    public function process(){ 

if (isset($_GET["oauth_problem"]) && $_GET["oauth_problem"] <> "") { 
    // in case if user cancel the login. redirect back to home page. 
    $_SESSION["err_msg"] = $_GET["oauth_problem"]; 
    header("location:index.php"); 
    exit; 
} 


$baseURL = Configure::read('base_url').'/'; 
$callbackURL = Configure::read('base_url').'/Linkedin/process'; 
$linkedinApiKey = '81d97wl9r9j1lz';/*client_id*/ 
$linkedinApiSecret = 'Mva0zHkh5dVcBctJ';/*secret id*/ 
$linkedinScope = 'r_basicprofile r_emailaddress'; 

$client = new oauth_client_class; 

$client->debug = false; 
$client->debug_http = true; 
$client->redirect_uri = $callbackURL; 

$client->client_id = $linkedinApiKey; 
$application_line = __LINE__; 
$client->client_secret = $linkedinApiSecret; 
//echo 'heloo'; die(); 
if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0) 
    die('Please go to LinkedIn Apps page https://www.linkedin.com/secure/developer?newapp= , '. 
     'create an application, and in the line '.$application_line. 
     ' set the client_id to Consumer key and client_secret with Consumer secret. '. 
     'The Callback URL must be '.$client->redirect_uri).' Make sure you enable the '. 
     'necessary permissions to execute the API calls your application needs.'; 

/* API permissions 
*/ 
$client->scope = $linkedinScope; 
if (($success = $client->Initialize())) { 
    if (($success = $client->Process())) { 
    if (strlen($client->authorization_error)) { 
     $client->error = $client->authorization_error; 
     $success = false; 
    } elseif (strlen($client->access_token)) { 
     $success = $client->CallAPI(
      'http://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name,location,picture-url,public-profile-url,formatted-name)', 
      'GET', array(
      'format'=>'json' 
     ), array('FailOnAccessError'=>true), $user); 
    } 
    } 
    $success = $client->Finalize($success); 
} 
if ($client->exit) exit; 
if ($success) { 
    pr($user); exit(); 
    // $user_id = $db->checkUser($user); 
// $_SESSION['loggedin_user_id'] = $user_id; 
    //$_SESSION['user'] = $user; 
} else { 
    //$_SESSION["err_msg"] = $client->error; 
} 
//header("location:index.php"); 
//exit; 
    } 



} 


/* End Controller File code*/ 
+0

歡迎來到SO。請不要發佈代碼的答案。提供一些背景和/或解釋。請參閱:http://stackoverflow.com/help/how-to-answer –

-1

包括供應商檔案LinkedIn/oauth_client.php和路徑: - 應用程序/供應商/ LinkedIn/oauth_client.php

/** code here*/ 
    <?php 
    /* 
    * oauth_client.php 
    * 
    * @(#) $Id: oauth_client.php,v 1.108 2014/06/12 10:58:53 mlemos Exp $ 
    * 
    */ 
    require_once "http.php"; 
    class oauth_client_class 
    { 
     var $error = ''; 
     var $debug = false; 
     var $debug_http = false; 
     var $exit = false; 
     var $debug_output = ''; 
     var $debug_prefix = 'OAuth client: '; 
     var $server = ''; 
     var $configuration_file = 'oauth_configuration.json'; 
     var $request_token_url = ''; 
     var $dialog_url = ''; 
     var $offline_dialog_url = ''; 
     var $append_state_to_redirect_uri = ''; 
     var $access_token_url = ''; 
     var $oauth_version = '2.0'; 
    var $url_parameters = false; 
    var $authorization_header = true; 
    var $token_request_method = 'GET'; 
    var $signature_method = 'HMAC-SHA1'; 
    var $redirect_uri = ''; 
    var $client_id = ''; 
    var $client_secret = ''; 
    var $api_key = ''; 
    var $get_token_with_api_key = false; 
    var $scope = ''; 
    var $offline = false; 
    var $access_token = ''; 
    var $access_token_secret = ''; 
var $access_token_expiry = ''; 
    var $access_token_type = ''; 
var $default_access_token_type = ''; 
var $access_token_parameter = ''; 
var $access_token_response; 
    var $store_access_token_response = false; 
    var $access_token_authentication = ''; 
var $refresh_token = ''; 
var $access_token_error = ''; 
    var $authorization_error = ''; 
    var $response_status = 0; 
    var $oauth_username = ''; 
var $oauth_password = ''; 
    var $oauth_user_agent = 'PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.108 $)'; 
    Function SetError($error) 
     { 
     $this->error = $error; 
     if($this->debug) 
      $this->OutputDebug('Error: '.$error); 
     return(false); 
     } 
    Function SetPHPError($error, &$php_error_message) 
     { 
     if(IsSet($php_error_message) 
     && strlen($php_error_message)) 
      $error.=": ".$php_error_message; 
     return($this->SetError($error)); 
     } 
    Function OutputDebug($message) 
     { 
     if($this->debug) 
     { 
      $message = $this->debug_prefix.$message; 
      $this->debug_output .= $message."\n";; 
      error_log($message); 
     } 
     return(true); 
     } 

     Function GetRequestTokenURL(&$request_token_url) 
     { 
     $request_token_url = $this->request_token_url; 
     return(true); 
     } 

     Function GetDialogURL(&$url, $redirect_uri = '', $state = '') 
     { 
     $url = (($this->offline && strlen($this->offline_dialog_url)) ? $this->offline_dialog_url : $this->dialog_url); 
     if(strlen($url) === 0) 
      return $this->SetError('the dialog URL '.($this->offline ? 'for offline access ' : '').'is not defined for this server'); 
     $url = str_replace(
      '{REDIRECT_URI}', UrlEncode($redirect_uri), str_replace(
      '{STATE}', UrlEncode($state), str_replace(
      '{CLIENT_ID}', UrlEncode($this->client_id), str_replace(
      '{API_KEY}', UrlEncode($this->api_key), str_replace(
      '{SCOPE}', UrlEncode($this->scope), 
      $url))))); 
     return(true); 
     } 

     Function GetAccessTokenURL(&$access_token_url) 
     { 
     $access_token_url = str_replace('{API_KEY}', $this->api_key, $this->access_token_url); 
     return(true); 
     } 

     Function GetStoredState(&$state) 
     { 
     if(!function_exists('session_start')) 
      return $this->SetError('Session variables are not accessible in this PHP environment'); 
     if(session_id() === '' 
     && !session_start()) 
      return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); 
     if(IsSet($_SESSION['OAUTH_STATE'])) 
      $state = $_SESSION['OAUTH_STATE']; 
     else 
      $state = $_SESSION['OAUTH_STATE'] = time().'-'.substr(md5(rand().time()), 0, 6); 
     return(true); 
     } 

     Function GetRequestState(&$state) 
     { 
     $check = (strlen($this->append_state_to_redirect_uri) ? $this->append_state_to_redirect_uri : 'state'); 
     $state = (IsSet($_GET[$check]) ? $_GET[$check] : null); 
     return(true); 
     } 

     Function GetRequestCode(&$code) 
     { 
     $code = (IsSet($_GET['code']) ? $_GET['code'] : null); 
     return(true); 
     } 

     Function GetRequestError(&$error) 
     { 
     $error = (IsSet($_GET['error']) ? $_GET['error'] : null); 
     return(true); 
     } 

     Function GetRequestDenied(&$denied) 
     { 
     $denied = (IsSet($_GET['denied']) ? $_GET['denied'] : null); 
     return(true); 
     } 

     Function GetRequestToken(&$token, &$verifier) 
     { 
     $token = (IsSet($_GET['oauth_token']) ? $_GET['oauth_token'] : null); 
     $verifier = (IsSet($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : null); 
     return(true); 
     } 

     Function GetRedirectURI(&$redirect_uri) 
     { 
     if(strlen($this->redirect_uri)) 
      $redirect_uri = $this->redirect_uri; 
     else 
      $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
     return true; 
     } 

     Function Redirect($url) 
     { 
     Header('HTTP/1.0 302 OAuth Redirection'); 
     Header('Location: '.$url); 
     } 

     Function StoreAccessToken($access_token) 
     { 
     if(!function_exists('session_start')) 
      return $this->SetError('Session variables are not accessible in this PHP environment'); 
     if(session_id() === '' 
     && !session_start()) 
      return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); 
     if(!$this->GetAccessTokenURL($access_token_url)) 
      return false; 
     $_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url] = $access_token; 
     return true; 
     } 

     Function GetAccessToken(&$access_token) 
     { 
     if(!function_exists('session_start')) 
      return $this->SetError('Session variables are not accessible in this PHP environment'); 
     if(session_id() === '' 
     && !session_start()) 
      return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); 
     if(!$this->GetAccessTokenURL($access_token_url)) 
      return false; 
     if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url])) 
      $access_token = $_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url]; 
     else 
      $access_token = array(); 
     return true; 
     } 
    Function ResetAccessToken() 
     { 
     if(!$this->GetAccessTokenURL($access_token_url)) 
      return false; 
     if($this->debug) 
      $this->OutputDebug('Resetting the access token status for OAuth server located at '.$access_token_url); 
     if(!function_exists('session_start')) 
      return $this->SetError('Session variables are not accessible in this PHP environment'); 
     if(session_id() === '' 
     && !session_start()) 
      return($this->SetPHPError('it was not possible to start the PHP session', $php_errormsg)); 
     if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url])) 
      Unset($_SESSION['OAUTH_ACCESS_TOKEN'][$access_token_url]); 
     return true; 
     } 

     Function Encode($value) 
     { 
     return(is_array($value) ? $this->EncodeArray($value) : str_replace('%7E', '~', str_replace('+',' ', RawURLEncode($value)))); 
     } 

     Function EncodeArray($array) 
     { foreach($array as $key => $value) 
      $array[$key] = $this->Encode($value); 
     return $array; 
     } 

     Function HMAC($function, $data, $key) 
     { 
     switch($function) 
     { 
      case 'sha1': 
      $pack = 'H40'; 
      break; 
      default: 
      if($this->debug) 
       $this->OutputDebug($function.' is not a supported an HMAC hash type'); 
      return(''); 
     } 
     if(strlen($key) > 64) 
      $key = pack($pack, $function($key)); 
     if(strlen($key) < 64) 
      $key = str_pad($key, 64, "\0"); 
     return(pack($pack, $function((str_repeat("\x5c", 64)^$key).pack($pack, $function((str_repeat("\x36", 64)^$key).$data))))); 
     } 

     continue...... 
0
  Function ProcessToken1($oauth, &$access_token) 
      { 
      if(!$this->GetAccessTokenURL($url)) 
       return false; 
      $options = array('Resource'=>'OAuth access token'); 
      $method = strtoupper($this->token_request_method); 
      switch($method) 
      { 
       case 'GET': 
       break; 
       case 'POST': 
       $options['PostValuesInURI'] = true; 
       break; 
       default: 
       $this->error = $method.' is not a supported method to request tokens'; 
       return false; 
      } 
      if(!$this->SendAPIRequest($url, $method, array(), $oauth, $options, $response)) 
       return false; 
      if(strlen($this->access_token_error)) 
      { 
       $this->authorization_error = $this->access_token_error; 
       return true; 
      } 
      if(!IsSet($response['oauth_token']) 
      || !IsSet($response['oauth_token_secret'])) 
      { 
       $this->authorization_error= 'it was not returned the access token and secret'; 
       return true; 
      } 
      $access_token = array(
       'value'=>$response['oauth_token'], 
       'secret'=>$response['oauth_token_secret'], 
       'authorized'=>true 
      ); 
      if(IsSet($response['oauth_expires_in']) 
      && $response['oauth_expires_in'] == 0) 
      { 
       if($this->debug) 
       $this->OutputDebug('Ignoring access token expiry set to 0'); 
       $this->access_token_expiry = ''; 
      } 
      elseif(IsSet($response['oauth_expires_in'])) 
      { 
       $expires = $response['oauth_expires_in']; 
       if(strval($expires) !== strval(intval($expires)) 
       || $expires <= 0) 
       return($this->SetError('OAuth server did not return a supported type of access token expiry time')); 
       $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires); 
       if($this->debug) 
       $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC'); 
       $access_token['expiry'] = $this->access_token_expiry; 
      } 
      else 
       $this->access_token_expiry = ''; 
      if(IsSet($response['oauth_session_handle'])) 
      { 
       $access_token['refresh'] = $response['oauth_session_handle']; 
       if($this->debug) 
       $this->OutputDebug('Refresh token: '.$access_token['refresh']); 
      } 
      return $this->StoreAccessToken($access_token); 
      } 

      Function ProcessToken2($code, $refresh) 
      { 
      if(!$this->GetRedirectURI($redirect_uri)) 
       return false; 
      $authentication = $this->access_token_authentication; 
      if(strlen($this->oauth_username)) 
      { 
       $values = array(
       'grant_type'=>'password', 
       'username'=>$this->oauth_username, 
       'password'=>$this->oauth_password 
      ); 
       $authentication = 'Basic'; 
      } 
      elseif($refresh) 
      { 
       $values = array(
       'refresh_token'=>$this->refresh_token, 
       'grant_type'=>'refresh_token', 
       'scope'=>$this->scope, 
      ); 
      } 
      else 
      { 
       $values = array(
       'code'=>$code, 
       'redirect_uri'=>$redirect_uri, 
       'grant_type'=>'authorization_code' 
      ); 
      } 
      $options = array(
       'Resource'=>'OAuth '.($refresh ? 'refresh' : 'access').' token', 
       'ConvertObjects'=>true 
      ); 
      switch(strtolower($authentication)) 
      { 
       case 'basic': 
       $options['AccessTokenAuthentication'] = $authentication; 
       $values['redirect_uri'] = $redirect_uri; 
       break; 
       case '': 
       $values['client_id'] = $this->client_id; 
       $values['client_secret'] = ($this->get_token_with_api_key ? $this->api_key : $this->client_secret); 
       break; 
       default: 
       return($this->SetError($authentication.' is not a supported authentication mechanism to retrieve an access token')); 
      } 
      if(!$this->GetAccessTokenURL($access_token_url)) 
       return false; 
      if(!$this->SendAPIRequest($access_token_url, 'POST', $values, null, $options, $response)) 
       return false; 
      if(strlen($this->access_token_error)) 
      { 
       $this->authorization_error = $this->access_token_error; 
       return true; 
      } 
      if(!IsSet($response['access_token'])) 
      { 
       if(IsSet($response['error'])) 
       { 
       $this->authorization_error = 'it was not possible to retrieve the access token: it was returned the error: '.$response['error']; 
       return true; 
       } 
       return($this->SetError('OAuth server did not return the access token')); 
      } 
      $access_token = array(
       'value'=>($this->access_token = $response['access_token']), 
       'authorized'=>true, 
      ); 
      if($this->store_access_token_response) 
       $access_token['response'] = $this->access_token_response = $response; 
      if($this->debug) 
       $this->OutputDebug('Access token: '.$this->access_token); 
      if(IsSet($response['expires_in']) 
      && $response['expires_in'] == 0) 
      { 
       if($this->debug) 
       $this->OutputDebug('Ignoring access token expiry set to 0'); 
       $this->access_token_expiry = ''; 
      } 
      elseif(IsSet($response['expires']) 
      || IsSet($response['expires_in'])) 
      { 
       $expires = (IsSet($response['expires']) ? $response['expires'] : $response['expires_in']); 
       if(strval($expires) !== strval(intval($expires)) 
       || $expires <= 0) 
       return($this->SetError('OAuth server did not return a supported type of access token expiry time')); 
       $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires); 
       if($this->debug) 
       $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC'); 
       $access_token['expiry'] = $this->access_token_expiry; 
      } 
      else 
       $this->access_token_expiry = ''; 
      if(IsSet($response['token_type'])) 
      { 
       $this->access_token_type = $response['token_type']; 
       if(strlen($this->access_token_type) 
       && $this->debug) 
       $this->OutputDebug('Access token type: '.$this->access_token_type); 
       $access_token['type'] = $this->access_token_type; 
      } 
      else 
      { 
       $this->access_token_type = $this->default_access_token_type; 
       if(strlen($this->access_token_type) 
       && $this->debug) 
       $this->OutputDebug('Assumed the default for OAuth access token type which is '.$this->access_token_type); 
      } 
      if(IsSet($response['refresh_token'])) 
      { 
       $this->refresh_token = $response['refresh_token']; 
       if($this->debug) 
       $this->OutputDebug('Refresh token: '.$this->refresh_token); 
       $access_token['refresh'] = $this->refresh_token; 
      } 
      elseif(strlen($this->refresh_token)) 
      { 
       if($this->debug) 
       $this->OutputDebug('Reusing previous refresh token: '.$this->refresh_token); 
       $access_token['refresh'] = $this->refresh_token; 
      } 
      return $this->StoreAccessToken($access_token); 
      } 
      Function RetrieveToken(&$valid) 
      { 
      $valid = false; 
      if(!$this->GetAccessToken($access_token)) 
       return false; 
      if(IsSet($access_token['value'])) 
      { 
       $this->access_token_expiry = ''; 
       $expired = (IsSet($access_token['expiry']) && strcmp($this->access_token_expiry = $access_token['expiry'], gmstrftime('%Y-%m-%d %H:%M:%S')) < 0); 
       if($expired) 
       { 
       if($this->debug) 
        $this->OutputDebug('The OAuth access token expired in '.$this->access_token_expiry); 
       } 
       $this->access_token = $access_token['value']; 
       if(!$expired 
       && $this->debug) 
       $this->OutputDebug('The OAuth access token '.$this->access_token.' is valid'); 
       if(IsSet($access_token['type'])) 
       { 
       $this->access_token_type = $access_token['type']; 
       if(strlen($this->access_token_type) 
       && !$expired 
       && $this->debug) 
        $this->OutputDebug('The OAuth access token is of type '.$this->access_token_type); 
       } 
       else 
       { 
       $this->access_token_type = $this->default_access_token_type; 
       if(strlen($this->access_token_type) 
       && !$expired 
       && $this->debug) 
        $this->OutputDebug('Assumed the default for OAuth access token type which is '.$this->access_token_type); 
       } 
       if(IsSet($access_token['secret'])) 
       { 
       $this->access_token_secret = $access_token['secret']; 
       if($this->debug 
       && !$expired) 
        $this->OutputDebug('The OAuth access token secret is '.$this->access_token_secret); 
       } 
       if(IsSet($access_token['refresh'])) 
       $this->refresh_token = $access_token['refresh']; 
       else 
       $this->refresh_token = ''; 
       $this->access_token_response = (($this->store_access_token_response && IsSet($access_token['response'])) ? $access_token['response'] : null); 
       $valid = true; 
      } 
      return true; 
      } 
      Function CallAPI($url, $method, $parameters, $options, &$response) 
      { 
      if(!IsSet($options['Resource'])) 
       $options['Resource'] = 'API call'; 
      if(!IsSet($options['ConvertObjects'])) 
       $options['ConvertObjects'] = false; 
      if(strlen($this->access_token) === 0) 
      { 
       if(!$this->RetrieveToken($valid)) 
       return false; 
       if(!$valid) 
       return $this->SetError('the access token is not set to a valid value'); 
      } 
      switch(intval($this->oauth_version)) 
      { 
       case 1: 
       if(strlen($this->access_token_expiry) 
       && strcmp($this->access_token_expiry, gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0) 
       { 
        if(strlen($this->refresh_token) === 0) 
        return($this->SetError('the access token expired and no refresh token is available')); 
        if($this->debug) 
        $this->OutputDebug('Refreshing the OAuth access token expired on '.$this->access_token_expiry); 
        $oauth = array(
        'oauth_token'=>$this->access_token, 
        'oauth_session_handle'=>$this->refresh_token 
       ); 
        if(!$this->ProcessToken1($oauth, $access_token)) 
        return false; 
        if(IsSet($options['FailOnAccessError']) 
        && $options['FailOnAccessError'] 
        && strlen($this->authorization_error)) 
        { 
        $this->error = $this->authorization_error; 
        return false; 
        } 
        if(!IsSet($access_token['authorized']) 
        || !$access_token['authorized']) 
        return($this->SetError('failed to obtain a renewed the expired access token')); 
        $this->access_token = $access_token['value']; 
        $this->access_token_secret = $access_token['secret']; 
        if(IsSet($access_token['refresh'])) 
        $this->refresh_token = $access_token['refresh']; 
       } 
       $oauth = array(
        (strlen($this->access_token_parameter) ? $this->access_token_parameter : 'oauth_token')=>((IsSet($options['2Legged']) && $options['2Legged']) ? '' : $this->access_token) 
       ); 
       break; 

       case 2: 
       if(strlen($this->access_token_expiry) 
       && strcmp($this->access_token_expiry, gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0) 
       { 
        if(strlen($this->refresh_token) === 0) 
        return($this->SetError('the access token expired and no refresh token is available')); 
        if($this->debug) 
        $this->OutputDebug('Refreshing the OAuth access token expired on '.$this->access_token_expiry); 
        if(!$this->ProcessToken2(null, true)) 
        return false; 
        if(IsSet($options['FailOnAccessError']) 
        && $options['FailOnAccessError'] 
        && strlen($this->authorization_error)) 
        { 
        $this->error = $this->authorization_error; 
        return false; 
        } 
       } 
       $oauth = null; 
       if(strcasecmp($this->access_token_type, 'Bearer')) 
        $url .= (strcspn($url, '?') < strlen($url) ? '&' : '?').(strlen($this->access_token_parameter) ? $this->access_token_parameter : 'access_token').'='.UrlEncode($this->access_token); 
       break; 

       default: 
       return($this->SetError($this->oauth_version.' is not a supported version of the OAuth protocol')); 
      } 
      return($this->SendAPIRequest($url, $method, $parameters, $oauth, $options, $response)); 
      } 
     Function Initialize() 
      { 
      $this->oauth_version = 
      $this->dialog_url = 
      $this->access_token_url = 
      $this->request_token_url = 
      $this->append_state_to_redirect_uri = ''; 
      $this->authorization_header = true; 
      $this->url_parameters = false; 
      $this->token_request_method = 'GET'; 
      $this->signature_method = 'HMAC-SHA1'; 
      $this->access_token_authentication = ''; 
      $this->access_token_parameter = ''; 
      $this->default_access_token_type = ''; 
      $this->store_access_token_response = false; 

      $this->oauth_version = '1.0a'; 
      $this->request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken?scope={SCOPE}'; 
      $this->dialog_url = 'https://api.linkedin.com/uas/oauth/authenticate'; 
      $this->access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'; 
      $this->url_parameters = true; 


      return(true); 
      } 

Function Process() 
     { 
     if(strlen($this->access_token) 
     || strlen($this->access_token_secret)) 
     { 
      if($this->debug) 
      $this->OutputDebug('The Process function should not be called again if the OAuth token was already set manually'); 
      return $this->SetError('the OAuth token was already set'); 
     } 
     switch(intval($this->oauth_version)) 
     { 
      case 1: 
      $one_a = ($this->oauth_version === '1.0a'); 
      if($this->debug) 
       $this->OutputDebug('Checking the OAuth token authorization state'); 
      if(!$this->GetAccessToken($access_token)) 
       return false; 
      if(IsSet($access_token['expiry'])) 
       $this->access_token_expiry = $access_token['expiry']; 
      if(IsSet($access_token['authorized']) 
      && IsSet($access_token['value'])) 
      { 
       $expired = (IsSet($access_token['expiry']) && strcmp($access_token['expiry'], gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0); 
       if(!$access_token['authorized'] 
       || $expired) 
       { 
       if($this->debug) 
       { 
        if($expired) 
        $this->OutputDebug('The OAuth token expired on '.$access_token['expiry'].'UTC'); 
        else 
        $this->OutputDebug('The OAuth token is not yet authorized'); 
        $this->OutputDebug('Checking the OAuth token and verifier'); 
       } 
       if(!$this->GetRequestToken($token, $verifier)) 
        return false; 
       if(!IsSet($token) 
       || ($one_a 
       && !IsSet($verifier))) 
       { 
        if(!$this->GetRequestDenied($denied)) 
        return false; 
        if(IsSet($denied) 
        && $denied === $access_token['value']) 
        { 
        if($this->debug) 
         $this->OutputDebug('The authorization request was denied'); 
        $this->authorization_error = 'the request was denied'; 
        return true; 
        } 
        else 
        { 
        if($this->debug) 
         $this->OutputDebug('Reset the OAuth token state because token and verifier are not both set'); 
        $access_token = array(); 
        } 
       } 
       elseif($token !== $access_token['value']) 
       { 
        if($this->debug) 
        $this->OutputDebug('Reset the OAuth token state because token does not match what as previously retrieved'); 
        $access_token = array(); 
       } 
       else { 
        $this->access_token_secret = $access_token['secret']; 
        $oauth = array(
        'oauth_token'=>$token, 
       ); 
        if($one_a) 
        $oauth['oauth_verifier'] = $verifier; 
        if(!$this->ProcessToken1($oauth, $access_token)) 
        return false; 
        if($this->debug) 
        $this->OutputDebug('The OAuth token was authorized'); 
       } 
       } 
       elseif($this->debug) 
       $this->OutputDebug('The OAuth token was already authorized'); 
       if(IsSet($access_token['authorized']) 
       && $access_token['authorized']) 
       { 
       $this->access_token = $access_token['value']; 
       $this->access_token_secret = $access_token['secret']; 
       if(IsSet($access_token['refresh'])) 
        $this->refresh_token = $access_token['refresh']; 
       return true; 
       } 
      } else { 
       if($this->debug) 
       $this->OutputDebug('The OAuth access token is not set'); 
       $access_token = array(); 
      } 
      if(!IsSet($access_token['authorized'])) 
      { 
       if($this->debug) 
       $this->OutputDebug('Requesting the unauthorized OAuth token'); 
       if(!$this->GetRequestTokenURL($url)) 
       return false; 
       $url = str_replace('{SCOPE}', UrlEncode($this->scope), $url); 
       if(!$this->GetRedirectURI($redirect_uri)) 
       return false; 
       $oauth = array(
       'oauth_callback'=>$redirect_uri, 
      ); 
       $options = array(
       'Resource'=>'OAuth request token', 
       'FailOnAccessError'=>true 
      ); 
       $method = strtoupper($this->token_request_method); 
       switch($method) 
       { 
       case 'GET': 
        break; 
       case 'POST': 
        $options['PostValuesInURI'] = true; 
        break; 
       default: 
        $this->error = $method.' is not a supported method to request tokens'; 
        break; 
       } 
       if(!$this->SendAPIRequest($url, $method, array(), $oauth, $options, $response)) 
       return false; 
       if(strlen($this->access_token_error)) 
       { 
       $this->authorization_error = $this->access_token_error; 
       return true; 
       } 
       if(!IsSet($response['oauth_token']) 
       || !IsSet($response['oauth_token_secret'])) 
       { 
       $this->authorization_error = 'it was not returned the requested token'; 
       return true; 
       } 
       $access_token = array(
       'value'=>$response['oauth_token'], 
       'secret'=>$response['oauth_token_secret'], 
       'authorized'=>false 
      ); 
       if(IsSet($response['login_url'])) 
       $access_token['login_url'] = $response['login_url']; 
       if(!$this->StoreAccessToken($access_token)) 
       return false; 
      } 
      if(!$this->GetDialogURL($url)) 
       return false; 
      if($url === 'automatic') 
      { 
       if(!IsSet($access_token['login_url'])) 
       return($this->SetError('The request token response did not automatically the login dialog URL as expected')); 
       if($this->debug) 
       $this->OutputDebug('Dialog URL obtained automatically from the request token response: '.$url); 
       $url = $access_token['login_url']; 
      } 
      else 
       $url .= (strpos($url, '?') === false ? '?' : '&').'oauth_token='.$access_token['value']; 
      if(!$one_a) 
      { 
       if(!$this->GetRedirectURI($redirect_uri)) 
       return false; 
       $url .= '&oauth_callback='.UrlEncode($redirect_uri); 
      } 
      if($this->debug) 
       $this->OutputDebug('Redirecting to OAuth authorize page '.$url); 
      $this->Redirect($url); 
      $this->exit = true; 
      return true; 
      case 2: 
      if($this->debug) 
      { 
       if(!$this->GetAccessTokenURL($access_token_url)) 
       return false; 
       $this->OutputDebug('Checking if OAuth access token was already retrieved from '.$access_token_url); 
      } 
      if(!$this->RetrieveToken($valid)) 
       return false; 
      if($valid) 
       return true; 
      if(strlen($this->oauth_username)) 
      { 
       if($this->debug) 
       $this->OutputDebug('Getting the access token using the username and password'); 
       return $this->ProcessToken2(null, false); 
      } 
      if($this->debug) 
       $this->OutputDebug('Checking the authentication state in URI '.$_SERVER['REQUEST_URI']); 
      if(!$this->GetStoredState($stored_state)) 
       return false; 
      if(strlen($stored_state) == 0) 
       return($this->SetError('it was not set the OAuth state')); 
      if(!$this->GetRequestState($state)) 
       return false; 
      if($state === $stored_state) 
      { 
       if($this->debug) 
       $this->OutputDebug('Checking the authentication code'); 
       if(!$this->GetRequestCode($code)) 
       return false; 
       if(strlen($code) == 0) 
       { 
       if(!$this->GetRequestError($this->authorization_error)) 
        return false; 
       if(IsSet($this->authorization_error)) 
       { 
        if($this->debug) 
        $this->OutputDebug('Authorization failed with error code '.$this->authorization_error); 
        switch($this->authorization_error) 
        { 
        case 'invalid_request': 
        case 'unauthorized_client': 
        case 'access_denied': 
        case 'unsupported_response_type': 
        case 'invalid_scope': 
        case 'server_error': 
        case 'temporarily_unavailable': 
        case 'user_denied': 
         return true; 
        default: 
         return($this->SetError('it was returned an unknown OAuth error code')); 
        } 
       } 
       return($this->SetError('it was not returned the OAuth dialog code')); 
       } 
       if(!$this->ProcessToken2($code, false)) 
       return false; 
      } else { if(!$this->GetRedirectURI($redirect_uri)) 
       return false; 
       if(strlen($this->append_state_to_redirect_uri)) 
       $redirect_uri .= (strpos($redirect_uri, '?') === false ? '?' : '&').$this->append_state_to_redirect_uri.'='.$stored_state; 
       if(!$this->GetDialogURL($url, $redirect_uri, $stored_state)) 
       return false; 
       if(strlen($url) == 0) 
       return($this->SetError('it was not set the OAuth dialog URL')); 
       if($this->debug) 
       $this->OutputDebug('Redirecting to OAuth Dialog '.$url); 
       $this->Redirect($url); 
       $this->exit = true; 
      } 
      break; 
    default: 
      return($this->SetError($this->oauth_version.' is not a supported version of the OAuth protocol')); 
     } 
     return(true); 
     } 
    continue.... 
+0

函數GetLine(){for($ line =「」;;){if($ this-> use_curl){ $ eol = strpos($這 - >響應, 「\ n」,$這 - > read_response); $ data =($ eol?substr($ this-> response,$ this-> read_response,$ eol + 1- $ this-> read_response):「」);如果(feof($ this-> connection)){this-> SetDataAccessError(「從HTTP服務器連接讀取時到達數據的末尾」),則返回$ this-> read_response + = strlen($ data);} else else { ); return(0);} – user6711924

+0

$ data = fgets($ this-> connection,100);} if(GetType($ data)!=「string」 || strlen($ data)== 0){ $ this-> SetDataAccessError(「不可能從HTTP服務器讀取行」); return(0); } $ line。= $ data; $ length = strlen($ line); ($長度 &&!strcmp(substr($ line,$ length-1,1),「\ n」)) {$ length - =(($ length> = 2 &&!strcmp(substr($ line ,$ length-2,1),「\ r」))?2:1); $ line = substr($ line,0,$ length);如果($ this-> debug) $ this-> OutputDebug(「S $ line」); return($ line);}}} – user6711924

+0

請使用[編輯按鈕](http://stackoverflow.com/posts/39852812 /編輯)添加額外的位,發表評論沒有幫助。此外,如果您對代碼的作用以及如何解決OP的問題添加一些註釋,它會有所幫助。代碼傾銷沒有解釋通常是皺眉了! – Draken

0
Function Finalize($success) 
      { 
      return($success); 
      } 
     Function Output() 
      { 
      if(strlen($this->authorization_error) 
      || strlen($this->access_token_error) 
      || strlen($this->access_token)) 
      { 
     ?> 
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
     <html> 
     <head> 
     <title>OAuth client result</title> 
     </head> 
     <body> 
     <h1>OAuth client result</h1> 
     <?php if(strlen($this->authorization_error)){ ?> 
     <p>It was not possible to authorize the application.<?php 
       if($this->debug){ ?> 
     <br>Authorization error: <?php echo HtmlSpecialChars($this->authorization_error); }?></p> 
     <?php } elseif(strlen($this->access_token_error)){ ?> 
     <p>It was not possible to use the application access token. 
     <?php if($this->debug) { ?> 
     <br>Error: <?php echo HtmlSpecialChars($this->access_token_error); } ?></p> 
     <?php } elseif(strlen($this->access_token)){ ?> 
     <p>The application authorization was obtained successfully. 
     <?php if($this->debug) { ?> 
     <br>Access token: <?php echo HtmlSpecialChars($this->access_token); 
        if(IsSet($this->access_token_secret)) 
        { ?> 
     <br>Access token secret: <?php echo HtmlSpecialChars($this->access_token_secret);}} ?></p> 
     <?php if(strlen($this->access_token_expiry)){ ?> 
     <p>Access token expiry: <?php echo $this->access_token_expiry; ?> UTC</p><?php } } ?> 
     </body> 
     </html> 
     <?php }} }; 

     /*** END THIS FILE***/