Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silicontrip/b0c44a2b4ecd3bf914e115416b18d78b to your computer and use it in GitHub Desktop.
Save silicontrip/b0c44a2b4ecd3bf914e115416b18d78b to your computer and use it in GitHub Desktop.
PS vs PHP
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
}
@silicontrip
Copy link
Author

If I had to describe what using powershell is most like I'd say php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment