0

LinkedIn API に問題があります。正常に動作することもあれば、次のエラーが表示されることもあります。

Message: file_get_contents( https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&client_id=####&client_secret=####&code=AQTJH8Hm9K8gmriHaDPLbJm_-E8OnbsiUCZvz32Jv_wD6idTW7Se8v0dohVUH0m8zGWzfKkanCC_NT3smdkoykE0nF88nH-tntK35UqHH4LwgzfcNBc&redirect_uri=http%3A%2F%2Fpeerbriefmini. local%2Flinkedincontroller ) [function.file-get-contents]: ストリームを開くことができませんでした: HTTP 要求が失敗しました! HTTP/1.0 400 request#no_content_length

アプリIDとシークレットを取り出しました。それが時々うまくいく理由はありますか?

編集:codeigniterで動作するphpコードを追加しました

<?php
class Linkedincontroller extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->config->load('linkedin');
    $this->load->library('linkedin');
    $this->load->model('account_model');
}

public function index() {

    // Change these
    define('API_KEY', '###');
    define('API_SECRET', '##');
    define('REDIRECT_URI', base_url().'linkedincontroller');
    define('SCOPE', 'r_fullprofile r_emailaddress rw_nus r_basicprofile r_contactinfo');

    // You'll probably use a database
    session_name('dfsfsdfsdf');
    session_start();

    // OAuth 2 Control Flow
    if (isset($_GET['error'])) {
        // LinkedIn returned an error
        print $_GET['error'] . ': ' . $_GET['error_description'];
        exit;
    } elseif (isset($_GET['code'])) {
        // User authorized your application
        if ($_SESSION['state'] == $_GET['state']) {
            // Get token so you can make API calls
            $this->getAccessToken();
        } else {
            // CSRF attack? Or did you mix up your states?
            //exit;
        }
    } else { 
        if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
            // Token has expired, clear the state
            $_SESSION = array();
        }
        if (empty($_SESSION['access_token'])) {
            // Start authorization process
            $this->getAuthorizationCode();
        }
    }

    // Congratulations! You have a valid token. Now fetch your profile 
    $user = $this->fetch('GET', '/v1/people/~:(id,first-name,last-name,main-address,picture-url,public-profile-url,email-address,interests,skills,languages,certifications,educations,positions,courses)');
    $linkedin_id = $user['id'];

    if(isset($linkedin_id)) {

        //var_dump($user);
        $linkedin_id = $user['id'];
        $linkedin_url = $user['publicProfileUrl'];
        $first_name = $user['firstName'];
        $last_name = $user['lastName'];
        $email = $user['emailAddress'];
        $profile_picture = $user['pictureUrl'];
        $address = $user['mainAddress'];

        $this->account_model->insert_database('accounts', 
            array(
            'account_confirmed' => 1,
            'account_active' => 1,
            'account_level' => 'Parent',
            'account_role' => 'User',
            'account_type' => 'Referrer',
            'account_completed_level' => 1,
            'master_account' => 1,
            'account_holder' => $first_name . ' ' .$last_name,
            'email' => $email,
            'linkedin_id' => $linkedin_id 
            )
        );

        $account_id = $this->db->insert_id();

        $this->account_model->insert_database('profiles',
            array(
            'account_id' => $account_id,
            'profile_picture' => $profile_picture,
            'linkedin_url' => $linkedin_url,
            'address' => $address
            )
        );

        // set flash data
        $this->session->set_userdata(
            array('linkedin_id' => $linkedin_id,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email' => $email,
            'profile_picture' => $profile_picture,
            'residential_address' => $address)
        );
        // redirect back to reg page with profile data
        redirect('register');
    }else{
                $data['header_text'] = $this->account_model->header_text();
                $data['header_links'] = $this->account_model->header_links();
                $data['user_picture'] = '';
                $data['nickname'] = $this->account_model->user_nickname();

        $this->load->view('template/header', $data);
        $data['error_message'] = 'Unknown LinkedIn credentials.';
        $this->load->view('error', $data);
        $this->load->view('template/footer');
    }

}

// empty fields
private function empty_fields($value) {
    if(isset($value)) {
        return $value;  
    }else{
        return NULL;    
    }
}


// authorization code
private function getAuthorizationCode() {
    $params = array('response_type' => 'code',
                    'client_id' => API_KEY,
                    'scope' => SCOPE,
                    'state' => uniqid('', true), // unique long string
                    'redirect_uri' => REDIRECT_URI,
              );

    // Authentication request
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);

    // Needed to identify request when it returns to us
    $_SESSION['state'] = $params['state'];

    // Redirect user to authenticate
    header("Location: $url");
}

// get access token 
private function getAccessToken() {
    $params = array('grant_type' => 'authorization_code',
                    'client_id' => API_KEY,
                    'client_secret' => API_SECRET,
                    'code' => $_GET['code'],
                    'redirect_uri' => REDIRECT_URI,
              );

    // Access Token request
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);

    // Tell streams to make a POST request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => 'POST',
                        )
                    )
                );

    // Retrieve access token information
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    $token = json_decode($response);

    // Store access token and expiration time
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time

    return true;
}

// fetch
private function fetch($method, $resource, $body = '') {
    $params = array('oauth2_access_token' => $_SESSION['access_token'],
                    'format' => 'json',
              );

    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => $method,
                        )
                    )
                );


    // Hocus Pocus
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    return json_decode($response, true);
    //return json_decode($response, false);
}
}
?>
4

0 に答える 0