Skip to content

Commit

Permalink
weibo
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe committed Nov 12, 2019
1 parent bc78a73 commit f0cb59f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
38 changes: 34 additions & 4 deletions src/OauthAdaptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,49 @@ public function __construct($adaptor = '', $option = [])
}


/**
* verify the token
* @param string $id
* @param string $token
* @return mixed
*/
public function verify($id = '', $token = '')
{
if (!method_exists($this->adaptor, "verify")) {
throw new DefaultException("method is not found");
}

return $this->adaptor->verify($id, $token);
}

/**
* 1. getCode
* 2. getAccessToken
* this will redirect
* @return mixed
* @throws DefaultException
*/
public function getCode()
{
if (!method_exists($this->adaptor, "getCode")) {
throw new DefaultException("method is not found");
}

return $this->adaptor->getCode();
}


/**
* get access token
* @param string $code
* @return mixed
* @throws DefaultException
*/
public function oauth()
public function getAccessToken($code = '')
{
return $this->adaptor->oauth();
if (!method_exists($this->adaptor, "getAccessToken")) {
throw new DefaultException("method is not found");
}

return $this->adaptor->getAccessToken($code);
}

}
38 changes: 16 additions & 22 deletions src/Providers/Weibo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,11 @@ public function __construct(array $option)
}


public function oauth()
{
$code = isset($_GET["code"]) ? $_GET["code"] : null;
if (!$code) {
$this->getCode();
exit();
}

$response = $this->getAccessToken($code);
$result = [
"accessToken" => $response["access_token"],
"expires" => $response["expires_in"],
"uid" => $response["uid"],
];
return $result;
}


/**
* request code, this will redirect
* @see https://open.weibo.com/wiki/Oauth2/authorize
*/
private function getCode()
public function getCode()
{
$uri = "/oauth2/authorize";

Expand Down Expand Up @@ -84,8 +67,11 @@ private function getCode()
* @param string $code
* @return mixed
*/
private function getAccessToken($code = "")
public function getAccessToken($code = "")
{
if (!$code) {
throw new DefaultException("no code");
}
$uri = "/oauth2/access_token";
$params = [
"client_id" => $this->option["clientId"],
Expand All @@ -94,9 +80,17 @@ private function getAccessToken($code = "")
"code" => $code,
"redirect_uri" => $this->option["redirect"],
];
$json = $this->http->post($this->endpoint . $uri, $params);
$data = json_decode($json, true);
if (isset($data["error_code"])) {
throw new DefaultException($data["error_description"]);
}
$result = [
"accessToken" => $data["access_token"],
"expires" => $data["expires_in"],
"uid" => $data["uid"],
];

$response = $this->http->post($this->endpoint . $uri, $params);
$result = json_decode($response, true);
return $result;
}

Expand Down

0 comments on commit f0cb59f

Please # to comment.