create account

RE: Integrate Steemconnect V2 User Authorisation Into Any WordPress Website by steempytutorials

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @freetissues/re-steempytutorials-integrate-steemconnect-v2-user-authorisation-into-any-wordpress-website-20180731t104221224z

· @steempytutorials ·
$0.10
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>';
```
👍  
properties (23)
authorsteempytutorials
permlinkre-freetissues-re-steempytutorials-integrate-steemconnect-v2-user-authorisation-into-any-wordpress-website-20180731t135136668z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-07-31 13:51:36
last_update2018-07-31 13:51:36
depth2
children0
last_payout2018-08-07 13:51:36
cashout_time1969-12-31 23:59:59
total_payout_value0.079 HBD
curator_payout_value0.025 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,976
author_reputation31,094,047,689,691
root_title"Integrate Steemconnect V2 User Authorisation Into Any WordPress Website"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id66,640,643
net_rshares61,729,502,157
author_curate_reward""
vote details (1)