You are viewing a single comment's thread from:

RE: Integrate Steemconnect V2 User Authorisation Into Any WordPress Website

To give a bit more detail.

I have a file authentication.php

<?php
  if (isset($_GET['code'])) {

    // Params for POST request
    $data = array(
        'code' => $_GET['code'],
        'client_secret' => SC_CLIENT_SECRET
    );

    $payload = json_encode($data);

    // Prepare new cURL resource
    $ch = curl_init('https://steemconnect.com/api/oauth2/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    // Set HTTP Header for POST request
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload))
    );

    // Submit the POST request
    $result = curl_exec($ch);

    // Close cURL session handle
    curl_close($ch);

    $json = json_decode($result);
    $date = date("Y/m/d H:G:s", time() + $json->expires_in);

    // Add tokens to the database
    global $wpdb;
    $wpdb->query("INSERT INTO `steem_authorization` (`id`, `access_token`, " .
    "`user_login`, `expires_in`, `refresh_token`) VALUES (NULL, " .
    "'" . $json->access_token . "', '" . $json->username ."', '" .
    $date . "', '" . $json->refresh_token ."') ON DUPLICATE KEY UPDATE " .
    "`user_login`='" . $json->username ."';");
  }

?>

The client secret, I set that in wp-config.php

'client_secret' => SC_CLIENT_SECRET

Then inside the template file of the page I want to add a authorise button I have added the following code:

// import steemconnet authentication
include 'authenticate.php';

// see if current user is logged in and has authenticated voting
// permissions in the database
global $wpdb;
$current_user = wp_get_current_user();

This includes the file and get the username.

Then lasty I check if the user has already authorised the app if not it shows a button to do so.

echo '<div>';
// Check if user is logged in
if ($current_user->user_login == ''){
  echo '<div align="center"><br><h1>You need to <a href="https://ste' .
  'emautomated.eu/wp-login.php?action=wordpress_social_authenticate&' .
  'mode=login&provider=Steemconnect&redirect_to=' . get_permalink() .
  '">log in</a></h1></div>';
} else {
  // Check if logged in user has authorised the app
  $results = $wpdb->get_results('SELECT * FROM `steem_authorization` ' .
  'WHERE `user_login` = "' . $current_user->user_login . '"');
      if (count($results) == 0){
        echo '<div align="center"><h1><br>Authorize <font color=' .
        '"blue">@steemautomated</font> to vote for you.</h1><br><a ' .
        'class="maxbutton-1 maxbutton maxbutton-steemconnect" href=' .
        '"https://steemconnect.com/oauth2/authorize?client_id=' .
        'steemautomated&redirect_uri=' . get_permalink() .'&response_' .
        'type=code&scope=offline,vote"><span class="mb-text">' .
        'Authorize</span></a></div><br>';
  }
}
echo '</div>';

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64455.55
ETH 3147.84
USDT 1.00
SBD 3.94