我的代码设置与runkeeper的身份验证系统连接。 我有这个奇怪的问题,返回的令牌总是一个。
我现在已经挣扎了8个小时。我搞砸了所有可能的解决方案。也已经使用了fopen并获取文件内容而不是curl
任何人看到我做错了什么?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Runkeeper {
private $client_id= '89ede033eb224a1b85b000bb35779db9';
private $client_secret ='***';
private $auth_url ='https://runkeeper.com/apps/authorize';
private $access_token_url = 'https://runkeeper.com/apps/token';
private $de_auth_url = 'https://runkeeper.com/apps/de-authorize';
private $response_type = 'code';
private $redirect_uri = 'http://localhost/vicoin/index.php/auth/runkeeper_reg';
public function __construct() {
}
public function connect(){
$req = "https://runkeeper.com/apps/authorize?client_id=89ede033eb224a1b85b000bb35779db9&redirect_uri=http://localhost/vicoin/index.php/auth/runkeeper_reg&response_type=code";
return $req;
}
public function callback($codeR){
$grant_type ='authorization_code';
$code = $codeR;
$client_id = '89ede033eb224a1b85b000bb35779db9';
$client_secret = '**';
$redirect_uri2 = "http://localhost/vicoin/index.php/auth/runkeeper_reg";
/*
$url = 'https://runkeeper.com/app/token';
$data = array('grant_type' => $grant_type, 'code' => $code, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri2);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = fopen($url,'rb',false,$context);
//var_dump($http_response_header);
var_dump($result);
//print_r($data);
return $result;
*/
$params = http_build_query(array(
'grant_type' => 'authorization_code',
'code' => $codeR,
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => ($redirect_uri2 == '' ? $this->redirect_uri : $redirect_uri2)
));
$options = array(
CURLOPT_URL => $this->access_token_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); /* Added to avoid "error :SSL certificate problem, verify that the CA cert is OK" */
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$decoderesponse = json_decode($response);
if ($decoderesponse == null) {
$this->api_last_error = "getRunkeeperToken: bad response";
return(false);
}
elseif (!isset($decoderesponse->error)) {
if (isset($decoderesponse->access_token)) {
$this->access_token = $decoderesponse->access_token;
}
if (isset($decoderesponse->token_type)) {
$this->token_type = $decoderesponse->token_type;
}
return(true);
}
elseif ($decoderesponse->error == 'invalid_grant') {
header('Location: https://runkeeper.com/app/token?response_type=code&client_id='.$this->client_id.'&redirect_uri='.urlencode($this->redirect_uri), true, 302);
exit();
}
else {
$this->api_last_error = "getRunkeeperToken: ".$decoderesponse->error;
return(false);
}
var_dump($http_response_header);
}
}