Per Previous post  , I had to reserve engineer a method for accessing documents in Google drive via API from WordPress , I put the access to Offline so it wouldn’t need refreshing , however after a week it suddenly stopped working with 

invalid_grant

I put the API in testing mode which means 

Authorizations by a test user will expire seven days from the time of consent. If your OAuth client requests an offline access type and receives a refresh token, that token will also expire.

Changing this to “In production” looks like a lot of trouble 

However reading through the guides a Service account would fix this task

 

Create a service account and create a key for that service account

JSON key file should look like below

{
"type": "service_account",
"project_id": "xxxxxxxx",
"private_key_id": "xxxxxxxxx",
"private_key": "END PRIVATE KEY-----\n",
"client_email": "xxxxx@xxxxxxxx.iam.gserviceaccount.com",
"client_id": "xxxxxxxxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxxxxxx.iam.gserviceaccount.com"
}

Save this json as something like service.json

Share the folders in Google Drive with your service account xxxxx@xxxxxxxx.iam.gserviceaccount.com with the right permissions 

Remove the need for token.json


$client->setAuthConfig(__DIR__.’/service.json’);

GD Star Rating
loading...
GD Star Rating
loading...

This enables you to make a change , and if it doesn’t work , the config gets rolled back

config system global
set cfg-save revert
set cfg-revert-timeout 300
end

(Press Enter)

**Make Change**

If not issue then , change back

config system global
set cfg-save auto
end


(Press Enter)

GD Star Rating
loading...
GD Star Rating
loading...

Wireup to GPIO like so with Hardware pull down resistor ( or look at wiring to to GPIO ports over 8 as these start on pull down ) 

On Raspberry Pi

sudo nano /boot/config.txt

add the below to the end

dtoverlay=w1-gpio,gpiopin=4

Reboot PI ( sudo reboot ) 

Add the following ( blank OWFS path ) 

 

It shoud appear in Devices Automatically

GD Star Rating
loading...
GD Star Rating
loading...

Domoticz

Create Virtual Sensor

http://192.168.50.138:8080/json.htm?type=createvirtualsensor&idx=3&sensorname=Watermeter&sensortype=113

Change Virtual Sensor Sub Type

http://192.168.50.138:8080/json.htm?type=setused&idx=7&name=Gasmeter&switchtype=2&used=true

How to set Value of Device

http://192.168.1.31:8080/json.htm?type=command&param=udevice&idx=3&svalue=100

Dominitz not working on reboot

sudo service domoticz.sh restart

GD Star Rating
loading...
GD Star Rating
loading...

Recently received my first Raspberry Pie and needed to get input sensors, especially a reed switch and also testing a Hall Effect Sensor

Reed Switch with Hardware Pull Down ( Resistors ) 

What inputs can I use with my Raspberry Pi? Part 1 - Switches and  logic-level sensors

On the Pie this looks like

 

How To Use GPIO on the Raspberry Pi with C++

Reed Switch with Internal Pull Down Resistor 

see below , gpios 0-8 are pulled high, the rest are pulled low. They are all inputs. Those initial power on states can not be changed – it’s wired into the SoC. So if you are wanting to use below make sure you use GPIO over 8 so it can withstand a reboot

Hall Effect Sensor with Hardware Pull Down ( Resistors ) 

This has three pins ( Extra Digital Out )  so needs to be Wired differently , we just need to protect the GPIO Pins

 

 

I used GPIO’s 4,5,6

#enables GPIO 4
echo 4 > /sys/class/gpio/export

#marks 4 as input ( not Output )
sudo echo in > /sys/class/gpio/gpio4/direction

#Configure the gpios to trigger an interrupt on both edge ( An edge is the name of a transition from HIGH to LOW (falling edge) ) – LOW to HIGH (rising edge).
echo both > /sys/class/gpio/gpio4/edge

#ActiveType constant to select active high, which means that logical true corresponds with high voltage.  active low, which means that logical true corresponds with low voltage.  0 (false) or 1 (true)

echo 0 > /sys/class/gpio/gpio4/active_low

How to read the current value of GPIO

cat /sys/class/gpio/gpio4/value

raspi-gpio get 4

How to set GPIO Pull Down 

On Boot gpios 0-8 are pulled high, the rest are pulled low. They are all inputs. Those initial power on states can not be changed – it’s wired into the SoC

raspi-gpio set 4 pd

pu set GPIO in-pad pull up
pd set GPIO pin-pad pull down

 

 

 

 
GD Star Rating
loading...
GD Star Rating
loading...

Recently my GD Star Rating plugin stopped working on my site , Votes could not be cast anymore.

Using the Chrome Developer tab, I was able to see JS errors in the console

‘msie’ of undefined TypeError: Cannot read property ‘msie’ in 

Addubg the below code to the top of gd-star-rating/js/gdsr.js fixed it

jQuery.browser = {};

(function () {

    jQuery.browser.msie = false;

    jQuery.browser.version = 0;

    if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {

        jQuery.browser.msie = true;

        jQuery.browser.version = RegExp.$1;

    }

})();
GD Star Rating
loading...
GD Star Rating
loading...

Recently tried to backup a single disk of a new VM , however was failing with 

“Failed to collect VM disks. Failed to get shared disk extents. Failed to get virtual disk ” per below

7/12/2021 5:09:48 PM :: Error: Failed to collect VM (ID: CCCCCCCCCCCC) information. Failed to collect VM disks. Failed to get shared disk extents. Failed to get virtual disk (path: ‘\\CCCC.local\CCCCCCCCCCC\Virtual Machines\CCCCC02\Virtual Hard Disks\CCCCCCCC02-Pagefile.vhdx’) setting data. Job failed (‘Failed to get the disk information.

Failed to open attachment ‘\\CCCC.local\CCCCCCCCCCC\Virtual Machines\CCCCC02\Virtual Hard Disks\CCCCCCCC02-Pagefile.vhdx’. Error: ‘The process cannot

 

The drive I was backing up wasn’t even the drive it was erroring on. I saw someone has the error here however confirmed the disk wasn’t being used elsewhere. I migrated the VM to a new host and that fixed the issue

GD Star Rating
loading...
GD Star Rating
loading...

Navigate to the Drive you have your DFS replicated folder on in powershell

 CD D:\

md d:\emptyfolder

Robocopy.exe d:\emptyfolder “D:\System Volume Information\dfsr\Private” /purge

rd d:\emptyfolder

Remove-Item  -Path .\DFRS\ -Recurse -Force -Verbose

Check Replication status

Get-WmiObject -Namespace “root\Microsoft\Windows\DFSR” -Class msft_dfsrreplicatedfolderinfo -ComputerName SERVERNAME| ft replicatedfoldername,state -auto -wrap

GD Star Rating
loading...
GD Star Rating
loading...

Recently had to reverse engineer a Google Drive PHP API Script, and I thought I would document the process!

Before you go down the below path , you will need to make sure you can push your project In Production (Very Long winded )

Testing mode for your project (even in Offline mode ) the below Refresh and Access Tokens with Expire every 7 days , if not use service account

Credentials.JSON

Format of this file is like below

{"web":{"client_id":"xxxxxxxxxxxx","project_id":"xxxxxxxxxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxxxxxxxxxxxxxx","redirect_uris":["xxxxxxxxxxxxxx","xxxxxxxxxx"]}}

How do get this 

  1. Create a new google account for Google drive and enable that account for API access ( https://developers.google.com/drive/api/v3/enable-drive-api )
  2. Create OAuth client ID and Select Web Application , add the authorize URLs and create 
  3. Download Crdential.json of the client your just created

 

Token.JSON

This is what they can your Access Token , Format of this file is like below

{"access_token":"xxxxxxxxxxxxxxxxxx","expires_in":3599,"scope":"https:\/\/www.googleapis.com\/auth\/drive.metadata.readonly","token_type":"Bearer","created":1625147606,"refresh_token":"xxxxxxxxxxxxxxxxxxxxxx"}

The script used the existing code here https://developers.google.com/docs/api/quickstart/php

You can see that the script is supposed to retrieve the token.json file when you give it the crdential file, however when I ran this I got 

Notice: Use of undefined constant STDIN - assumed 'STDIN'

Warning: Use of undefined constant STDIN – assumed ‘STDIN’ (this will throw an Error in a future version of PHP) in 

Warning: fgets() expects parameter 1 to be resource, string given in on line 3

Further research shows “only the CLI (command line) SAPI defines I/O constants such as STDINSTDOUT, and STDERR

I didn’t have access to PHP Cli Easily , so I performed the below ( this was a big help but it pulled an Application Access Token instead of Web

To get this you have to post stuff to google api and get a file

  1. Open Up your Credential.json file and get your Client_id and Client Secret
  2. Change the below to have your Client ID and correct redirect_uri and scope 
    https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXXXXXX&redirect_uri=XXXXXXXXX&scope=https://www.googleapis.com/auth/drive.metadata.readonly&response_type=code&access_type=offline&prompt=consent
  3. Post this into a browser that’s logged into the google account where you API is registered Go through the prompts to allow access , after this is done it will take you back to your redirect_uri and give you an Authorisation code per below

  4. Format https://redirect_uri/?code=[Authorisaton Code]&scope=[SCOPE] ( Copy and save the Authorisation Code ) 
  5. Next you will need a Linux machine with Curl , you can install Linux on Windows using WSL
  6. Open up the Linux terminal and paste the below

    curl \

–request POST \
–data “code=[Authorisaton Code]&client_id=[CLIENT ID]&client_secret=]CLIENT SECRET]&redirect_uri=[REDIRECT URI]&grant_type=authorization_code” \
https://accounts.google.com/o/oauth2/token

It will come back with an access_token and refresh_token

create a Json file with this in per below 

{“access_token”:”xxxxxxxxxxxxxxxxxx”,”expires_in”:3599,”scope”:”https:\/\/www.googleapis.com\/auth\/drive.metadata.readonly”,”token_type”:”Bearer”,”created”:1625147606,”refresh_token”:”xxxxxxxxxxxxxxxxxxxxxx”}

How to Create Permissions so files are public

Folder or File ID in fileId

Request Body

{
“role”: “reader”,
“type”: “user”
}

GD Star Rating
loading...
GD Star Rating
loading...