Extract access token from the php linked callback value of Nearo (login with Naver ID)

Asked 2 years ago, Updated 2 years ago, 94 views

Hello, everyone There's a site that I'm making with php I'm going to insert the login button with NAVER ID

There is an example of linking with php source at Naver Developer Center

Example 1 - Request Naver Login Access Token

Naver Login Access Token Acquisition Example consists of two files. (naverlogin.php, callback.php)
1. naverlogin.php
<?php
  // Naver Login Access Token Request Example
  $client_id = "YOUR_CLIENT_ID";
  $redirectURI = urlencode("YOUR_CALLBACK_URL");
  $state = "RAMDOM_STATE";
  $apiURL = "https://nid.naver.com/oauth2.0/authorize?response_type=code&client_id=".$client_id."&redirect_uri=".$redirectURI."&state=".$state;
?><a href="<?php echo $apiURL ?>"><img height="50" src="http://static.nid.naver.com/oauth/small_g_in.PNG"/></a>

2. callback.php
<?php
  // Naver Login Callback Example
  $client_id = "YOUR_CLIENT_ID";
  $client_secret = "YOUR_CLIENT_SECRET";
  $code = $_GET["code"];;
  $state = $_GET["state"];;
  $redirectURI = urlencode("YOUR_CALLBACK_URL");
  $url = "https://nid.naver.com/oauth2.0/token?grant_type=authorization_code&client_id=".$client_id."&client_secret=".$client_secret."&redirect_uri=".$redirectURI."&code=".$code."&state=".$state;
  $is_post = false;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, $is_post);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $headers = array();
  $response = curl_exec ($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  echo "status_code:".$status_code."
";
  curl_close ($ch);
  if($status_code == 200) {
    echo $response;
  } } else {
    echo "Error Content:"$response;
  }
?>

The following values were printed from the file set to the callback address to which the source is applied

I'm done up to here

It is difficult to use the access_token value in the variable from the value received as callback It's another example

Example 2 - Member profile inquiry

// Naver API Example - Member profile inquiry
<?php
  $token = "YOUR_ACCESS_TOKEN";
  $header = "Bearer".$token; // Add space after Bearer
  $url = "https://openapi.naver.com/v1/nid/me";
  $is_post = false;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, $is_post);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $headers = array();
  $headers[] = "Authorization: ".$header;
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $response = curl_exec ($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  echo "status_code:".$status_code."<br>";
  curl_close ($ch);
  if($status_code == 200) {
    echo $response;
  } } else {
    echo "Error Content:"$response;
  }
?>

I think Example 1 Extract the access_token value from the contents received Put it in a variable called $token and send it to $_SESSION Example 2 I think you can get it from the member profile inquiry and inquire about the information

in the course of Example 1 Extract the access_token value from the contents received Put it in a variable called $token and send it to $_SESSION

I don't know how to code it

I thought it would be done quickly because there were many articles to refer to at the Naver Developer Center, but I've been grumbling for two days. Thank you for reading it

php sns-login

2022-09-21 17:12

1 Answers

Document The response format is json. $result = json_decode($response); to receive it as an array, and then check $result['access_token'].


2022-09-21 17:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.