1

私は、ここでの Twitter サインインのドキュメントに記載されている手順に従おうとしています: https://dev.twitter.com/docs/auth/implementing-sign-twitter

私のコード:

$oauth_consumer_secret = '***';
$access_token_secret = '***';

$oauth_consumer_key = '***';
$oauth_nonce = createNonce();
$oauth_signature_method = 'HMAC-SHA1';
$oauth_time = time();
$oauth_token = '***';
$oauth_version = '1.0';

$oauth = array(
    'oauth_callback' => '***',
    'oauth_consumer_key'=>$oauth_consumer_key,
    'oauth_nonce'=>$oauth_nonce,
    'oauth_signature_method'=>$oauth_signature_method,
    'oauth_timestamp'=>$oauth_time,
    'oauth_token'=>$oauth_token,
    'oauth_version'=>$oauth_version
);

$baseURI = 'https://api.twitter.com/1.1/oauth/request_token';
$baseString = buildBaseString($baseURI,$oauth);
$compositeKey = getCompositeKey($oauth_consumer_secret,null);
$oauth_signature = base64_encode(hash_hmac('sha1', $baseString, $compositeKey, true));
$oauth['oauth_signature'] = $oauth_signature; //add the signature to our oauth array

$header = array(buildAuthorizationHeader($oauth));
$login = loginUser($baseURI,$header);
echo $login;

function loginUser($baseURI,$header){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $baseURI);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    if ($output!=''){
        return $output;
    } else {
        return 'fail';
    };
};

function buildBaseString($baseURI,$params){
    $r = array(); // temp array
    ksort($params); // sorts params alphabetically by key
    foreach($params as $key=>$value){
        $r[] = '$key='.rawurlencode($value);
    };
    return 'POST&'.rawurlencode($baseURI).'&'.rawurlencode(implode('&', $r)); // returns complete base string
};

// Create composite key
function getCompositeKey($consumerSecret,$requestToken){
    return rawurlencode($consumerSecret) . '&' . rawurlencode($requestToken);
};

function createNonce(){
    $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $string = '';
    for ($i=0; $i<32; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
    };
    return $string;
};

function buildAuthorizationHeader($oauth){
    $r = 'Authorization: OAuth '; //header prefix
    $values = array(); //temporary key=value array
    foreach($oauth as $key=>$value)
    $values[] = "$key=\"" . rawurlencode($value) . "\""; //encode key=value string
    $r .= implode(', ', $values); //reassemble
    return $r; //return full authorization header
};

私が抱えている問題は、これまでに何の反応も得られないことです! そのため、ログイン機能は「失敗」を返し続けます。

curlopt_ssl_verifypeer を false に変更すると、HTTP/1.1 401 Unauthorized エラーが発生します。

ヘルプや手がかりをいただければ幸いです。

4

1 に答える 1