Created
July 22, 2019 03:39
-
-
Save silicontrip/b0c44a2b4ecd3bf914e115416b18d78b to your computer and use it in GitHub Desktop.
PS vs PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function getAccessToken($user,$pass) | |
{ | |
$url = SonyCI::$host ."/oauth2/token"; | |
# documentation say Password, but that fails password doesn't | |
$data = [ | |
"client_id" => $this->config->client_id, | |
"client_secret" => $this->config->client_secret, | |
"grant_type" => "password" ]; | |
$userPass = base64_encode($user.':'.$pass); | |
$contextArray = ["http"=> [ | |
"method" => "POST", | |
'ignore_errors' => true, | |
"header" => [ | |
"Content-Type: application/json" , | |
"Authorization: Basic " . $userPass | |
], | |
"content" => json_encode($data) | |
] ]; | |
$context = stream_context_create($contextArray); | |
$result = file_get_contents($url, false, $context); | |
$resobj = json_decode($result); | |
if (property_exists($resobj,"access_token")) | |
{ | |
$this->config->access_token = $resobj->access_token; | |
$this->config->expires = $resobj->expires_in + time(); | |
$this->config->refresh_token = $resobj->refresh_token; | |
return true; | |
} | |
return false; | |
} | |
[bool] GetAccessToken($user,$password) | |
{ | |
$url = [SonyCI]::host + "/oauth2/token" | |
$data = @{ | |
"client_id"=$this.config.client_id; | |
"client_secret"=$this.config.client_secret; | |
"grant_type"="password" | |
} | |
$pair = "$($user):$($password)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) | |
$basicAuthValue = "Basic $encodedCreds" | |
$headers = @{ "Content-Type"="application/json"; | |
"Authorization"=$basicAuthValue | |
} | |
$resobj=Invoke-RestMethod -Method 'Post' -Uri $url -Headers $headers -Body ($data | convertto-json) | |
if ($resobj.PSObject.Properties.name -match "access_token") { | |
$this.config.access_token = $resobj.access_token | |
$this.config.expires = (get-date).AddSeconds($resobj.expires_in) | |
$this.config.refresh_token = $resobj.refresh_token | |
return $true | |
} | |
return $false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I had to describe what using powershell is most like I'd say php.