Unable to get past the 400 Error - api

Code to create a new user:
function New-DropBoxUser {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[string]$FullName
)
# Split name into username #
$FirstName, $LastName = $FullName.split(' ')
$UserName = ($FirstName[0]+$LastName).toLower()
$email = "$UserName#mycomp.com"
$Body = #{
"new_members" = #(#{
"member_email" = $email;
"member_given_name" = $FirstName;
"member_surname" = $LastName;
"send_welcome_email" = "true";
"role" = #{
".tag" = "member_only"
}
})
}
Write-Host ''
Write-Host 'Creating DropBox User...' -ForegroundColor 'Yellow' -BackgroundColor 'Black'
$AuthToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
$token = "Bearer $AuthToken"
$response = Invoke-RestMethod `
-Method Post `
-Uri "https://api.dropboxapi.com/2/team/members/add" `
-Headers #{ Authorization = $token } `
-ContentType "application/json; charset=utf-8" `
-Body (ConvertTo-Json $Body)
}
Output:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
Anyone, please tell me the fix. I tried the Endpoint "team/members/list" to fetch user list using the same $token and it works.
I am totally new to Dropbox API so my code is borrowed from Google, while being good with PowerShell I tried to patch up using many different codes but none worked.

When all else fails, RTFM, whose parameters say:
{
"new_members": [
{
"member_email": "tom.s#company.com",
"member_given_name": "Tom",
"member_surname": "Silverstone",
"member_external_id": "company_id:342432",
"send_welcome_email": true,
"role": "member_only"
}
],
"force_async": false
}
Try removing .tag from the role definition and replace "true" with $true
$Body = #{
"new_members" = #(#{
"member_email" = $email
"member_given_name" = $FirstName
"member_surname" = $LastName
"send_welcome_email" = $true
"role" = "member_only"
})
}

Related

Why the Akeneo severals patch works with Postman and not with Guzzle

I have a problem on an API call with the Guzzle 7.4.3 library to PATCH severals produced on Akeneo 5.0.50
By following the documentation below:
https://api.akeneo.com/api-reference.html#patch_products
I first tested the api via Postman here is the result:
Res Postman
However with the guzzle code this one doesn't work and returns a null body with a 200 code.
Here is the code:
public function patchProductGlobalAkeneo(string $token_akeneo, string $data_products): object
{
try {
$request = $this->client->request('PATCH', $_ENV["AKENEO_API_URL"] . $_ENV["AKENEO_API_URL_PRODUCTS"], [
'headers' => [
'Authorization' => 'Bearer ' . $token_akeneo,
'Content-Type' => 'application/vnd.akeneo.collection+json'
],
'json' => json_decode('{"identifier":"01750003001400","values":{"stock":[{"data":"10","locale":null,"scope":null}]}}
{"identifier":"01750003001400","values":{"stock":[{"data":"10","locale":null,"scope":null}]}}')
]);
} catch (RequestException $e) {
$res = new stdClass();
$res->content = json_decode($e->getResponse()->getBody()->getContents());
$res->status_res = $e->getResponse()->getStatusCode();
var_dump($res->status_res);
var_dump("ERROR");
return $res;
}
$res = new stdClass();
$res->content = json_decode($request->getBody()->getContents());
$res->status_res = $request->getStatusCode();
var_dump($res->content);
var_dump($res->status_res);
var_dump("GOOD");
return $res;
}
The result of the var_dump : Res Var_dump

Azure DevOps API: Get all files (source and target) of Pull Request

I searched quite a bit, found several threads here (e.g. this, that, and more), but I could not find the answer to the following task:
Use the Azure DevOps API to retrieve the content changes (basically the file before and after) of all the files of a specific PR.
I can find a PR, can loop through changes, iterations, commits (in various combinations), but I have not been able to download both the first and the last version of each or the files (and there should be a way as I can view the before and after in a PR in DevOps).
Any hints where/how I can retrieve both versions of a file of a certain commit/change/iteration?
Many thanks in advance!
Cheers,
Udo
Thanks for all the hints. Looks like I managed to find a wat to pull it. Please feel free to correct my approach.
Here's the complete PowerShell file:
[CmdletBinding()]
param (
[int]
$pullRequestId,
[string]
$repoName
)
# --- your own values ---
$pat = 'your-personal access token for Azure DevOps'
$urlOrganization = 'your Azure DevOps organization'
$urlProject = 'your Azure DevOps project'
$basePath = "$($env:TEMP)/pullRequest/" # a location to store all the data
# --- your own values ---
if (!$repoName)
{
$userInput = Read-Host "Please enter the repository name"
if (!$userInput)
{
Write-Error "No repository name given."
exit
}
$repoName = $userInput
}
if (!$pullRequestId)
{
$userInput = Read-Host "Please enter the PullRequest ID for $($repoName)"
if (!$userInput)
{
Write-Error "No PullRequest ID given."
exit
}
$pullRequestId = $userInput
}
$prPath = "$($basePath)$($pullRequestId)"
$sourcePath = "$($basePath)$($pullRequestId)/before"
$targetPath = "$($basePath)$($pullRequestId)/after"
# --- helper methods ---
function CleanLocation([string]$toBeCreated)
{
RemoveLocation $toBeCreated
CreateLocation $toBeCreated
if (!(Test-Path $toBeCreated))
{
Write-Error "Path '$toBeCreated' could not be created"
}
}
function CreateLocation([string]$toBeCreated)
{
if (!(Test-Path $toBeCreated)) { New-Item -ErrorAction Ignore -ItemType directory -Path $toBeCreated > $null }
}
function RemoveLocation([string]$toBeDeleted)
{
if (Test-Path $toBeDeleted) { Remove-Item -Path $toBeDeleted -Recurse -Force }
}
function DeleteFile([string]$toBeDeleted)
{
if (Test-Path $toBeDeleted) { Remove-Item -Path $toBeDeleted -Force }
}
# --- helper methods ---
# --- Azure DevOps helper methods ---
function GetFromDevOps($url)
{
$patToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$repoHeader = #{ "Authorization" = "Basic $patToken" }
$response = $(Invoke-WebRequest $url -Headers $repoHeader)
if ($response.StatusCode -ne 200)
{
Write-Error "FAILED: $($response.StatusDescription)"
}
return $response
}
function JsonFromDevOps($url)
{
$response = GetFromDevOps $url
return ConvertFrom-Json -InputObject $response.Content
}
function DownloadFromDevOps($url, $filename)
{
$patToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$repoHeader = #{ "Authorization" = "Basic $patToken" }
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest $url -Headers $repoHeader -OutFile $filename
$ProgressPreference = 'Continue'
}
function DownloadAndSaveFile($itemUrl, $outputPath, $path, $overwrite)
{
$outFile = "$outputPath$($path)"
$fileExists = Test-Path $outFile
if (!$fileExists -or $overwrite)
{
$outPath = [System.IO.Path]::GetDirectoryName($outFile)
CreateLocation $outPath
if ($fileExists)
{
Write-Host " overwriting to $outputPath"
}
else
{
Write-Host " downloading to $outputPath"
}
DownloadFromDevOps $itemUrl $outFile
}
}
function DownloadFiles($changes, $beforePath, $beforeCommitId, $afterPath, $afterCommitId)
{
foreach ($change in $changes)
{
$item = $change.item
if ($item.isFolder)
{
continue;
}
$path = $item.path
$originalPath = $change.originalPath
if (!$path)
{
$path = $change.originalPath
}
$displayPath = $path ?? $originalPath
$changeType = $change.changeType
Write-Host "[$($changeType)] $($displayPath)"
if (($changeType -eq "edit, rename"))
{
$itemUrl = "$($urlRepository)/items?path=$($originalPath)&versionDescriptor.version=$($beforeCommitId)&versionDescriptor.versionOptions=0&versionDescriptor.versionType=2&download=true"
DownloadAndSaveFile $itemUrl $beforePath $originalPath
}
# just get the source/before version
if ($changeType -eq "delete")
{
$itemUrl = "$($urlRepository)/items?path=$($originalPath)&versionDescriptor.version=$($beforeCommitId)&versionDescriptor.versionOptions=0&versionDescriptor.versionType=2&download=true"
DownloadAndSaveFile $itemUrl $beforePath $originalPath
DeleteFile "$($afterPath)$($originalPath)"
}
if ($changeType -eq "edit")
{
$itemUrl = "$($urlRepository)/items?path=$($path)&versionDescriptor.version=$($beforeCommitId)&versionDescriptor.versionOptions=0&versionDescriptor.versionType=2&download=true"
DownloadAndSaveFile $itemUrl $beforePath $path
}
if (($changeType -eq "add") -or ($changeType -eq "edit") -or ($changeType -eq "edit, rename"))
{
$itemUrl = "$($urlRepository)/items?path=$($path)&versionDescriptor.version=$($afterCommitId)&versionDescriptor.versionOptions=0&versionDescriptor.versionType=2&download=true"
DownloadAndSaveFile $itemUrl $afterPath $path $true
}
$validChangeTypes = #('add', 'delete', 'edit', 'edit, rename')
if (!($validChangeTypes.Contains($changeType)))
{
Write-Warning "Unknown change type $($changeType)"
}
}
}
# --- Azure DevOps helper methods ---
$urlBase = "https://dev.azure.com/$($urlOrganization)/$($urlProject)/_apis"
$urlRepository = "$($urlBase)/git/repositories/$($repoName)"
$urlPullRequests = "$($urlRepository)/pullRequests"
$urlPullRequest = "$($urlPullRequests)/$($pullRequestId)"
$urlIterations = "$($urlPullRequest)/iterations"
CleanLocation $prPath
$iterations = JsonFromDevOps $urlIterations
foreach ($iteration in $iterations.value)
{
# the modified file
$srcId = $iteration.sourceRefCommit.commitId
# the original file
$comId = $iteration.commonRefCommit.commitId
$comId = $iteration.targetRefCommit.commitId
$urlIterationChanges = "$($urlIterations)/$($iteration.id)/changes"
$iterationChanges = JsonFromDevOps $urlIterationChanges
DownloadFiles $iterationChanges.changeEntries $sourcePath $comId $targetPath $srcId
}

Swiss post tracking web service implementation using php soap

I'm working on a E-commerce site on which orders are placed. i want to track that delivery status of order and I need to give a status of order to user over mail.
I have to use Swiss Post Tracking Web Service API. Can anyone show me how to do so? I am new to SOAP WSDL.
From my understanding i did this now how to go further? My code below its just basic client i need to:
I need to track the delivery status
Below is my code
$wsdl = "https://webservices.post.ch:17005/IN_MYPBxTT/services/TrackAndTraceDFU.ws?WSDL
$username = 'username';
$password = 'password';
$trace = True;
$soapConfig = array(
'login'=>$username,
'password'=>$password,
'trace'=>$trace,
'keep_alive'=>TRUE,
'encoding'=>'utf8',
'connection_timeout'=>90,
);
$soapClient = new SoapClient($wsdl, $soapConfig);
try {
$xml = '<soapenv:Envelope xmlns:soapenv=“http://schemas.xmlsoap.org/soap/envelope/“
xmlns:req=“http://www.post.ch/npp/trackandtracews/v02/shipmentssearch/req“>
<soapenv:Header/>
<soapenv:Body>
<req:ShipmentsSearch>
<language>de</language>
<ShipmentNumbers>
<ShipmentNumber>99.60.122566.27198756</ShipmentNumber>
</ShipmentNumbers>
<Identity>?</Identity>
<Version>2.4</Version>
</req:ShipmentsSearch>
</soapenv:Body>
</soapenv:Envelope>';
$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $this->_soapClient->__soapCall('ShipmentsSearch', $args);
return $res;
} catch (SoapFault $e) {
echo "Error: {$e}";
}
echo "<hr>Last Response";
echo "<pre>", htmlspecialchars($this->_soapClient->__getLastResponse()), "</pre>";
die;
I am using this code but getting below error
Error: SoapFault exception: [env:Client] Internal Error in /directory-name/ConsignmentWebService.php:114
Stack trace: #0 /directory-name/ConsignmentWebService.php(114): SoapClient->__soapCall('ShipmentsSearch', Array)
and get the response like this
Last Request
<?xml version='1.0' ?>
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Body>
<env:Fault>
<faultcode>env:Client</faultcode>
<faultstring>Internal Error</faultstring>
</env:Fault>
</env:Body>
</env:Envelope>
I referred this tutorial
https://www.post.ch/en/business-solutions/digital-commerce/notification-services/sendungsinformationen
please help me I am new in soap
Updated Code who are looking for solution
$wsdl = "https://webservices.post.ch:17005/IN_MYPBxTT/services/TrackAndTraceDFU.ws?WSDL
$username = 'username';
$password = 'password';
$trace = True;
$soapConfig = array(
'login'=>$username,
'password'=>$password,
'trace'=>$trace,
'keep_alive'=>TRUE,
'encoding'=>'utf8',
'connection_timeout'=>90,
);
$soapClient = new SoapClient($wsdl, $soapConfig);
$response = null;
$generateRequest = array(
'language'=>'de',
'ShipmentNumbers'=>array(
'ShipmentNumber'=>$tracking_code, //your track code
),
'Identity'=>'?',
'Version'=>2.4
);
try {
$response = $this->_soapClient->ShipmentsSearch($generateRequest);
return $response;
} catch (SoapFault $fault) {
echo('Error in Getting Delivery Status: '. $fault->__toString() .'<br />');
echo '<pre>';
var_dump($this->_soapClient->__getLastResponse());
echo '</pre>';
die();
}

How to use JSON to Powershell Invoke-RestMethod?

I am trying to invoke the API using this request. This request is working fine in Postman. But how can I convert it to PowerShell Invoke-RestMethod?
This is what I tried.
$token = "abcdef123456"
$url = "https://example.com/OVAV1/triggercalls/1"
$header = #{"Authorization"='abdcdef123456'}
$body =
{
"requestid": "<Echo back field>",
"templatename": "Line_Down",
"templateid": "abcde12345",
"noofrecords": 1,
"variableinfo": [
{
"<<MOBILE_NUMBER>>": "123456789",
"<<UNIQUE_CALL_IDENTIFIER>>": "A001",
"<<LinkType>>": "PRI",
"<<HostName>>": "HOSTNAME"
}
]
}
$request = Invoke-RestMethod -Uri $url -Headers $header -Body $body -ContentType "application/json" -Method Post
$request
I tried the JSON code in Postman. It's working fine. How can I use the same JSON code in PowerShell?
You can't put JSON data into PowerShell code like that. Put it in a here-string and it should work:
$body = #"
{
"requestid": "<Echo back field>",
"templatename": "Line_Down",
"templateid": "abcde12345",
"noofrecords": 1,
"variableinfo": [
{
"<<MOBILE_NUMBER>>": "123456789",
"<<UNIQUE_CALL_IDENTIFIER>>": "A001",
"<<LinkType>>": "PRI",
"<<HostName>>": "HOSTNAME"
}
]
}
"#
You could also define the data as a hashtable and convert it to a JSON string:
$data = #{
"requestid" = "<Echo back field>"
"templatename" = "Line_Down"
"templateid" = "abcde12345"
"noofrecords" = 1
"variableinfo" = #(
#{
"<<MOBILE_NUMBER>>" = "123456789"
"<<UNIQUE_CALL_IDENTIFIER>>" = "A001"
"<<LinkType>>" = "PRI"
"<<HostName>>" = "HOSTNAME"
}
)
}
$body = $data | ConvertTo-Json

POLONIEX API Connect

I'm trying to connect to POLONIEX API using powershell. I've tried different variation of the following code without any luck. Could anyone take a look to see what I'm missing?
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uri = 'https://poloniex.com/tradingApi'
$key = 'POLO-API-SECRET-KEY'
$secret= 'poloapisecret'
$nonce = [int][double]::Parse((Get-Date (get-date).touniversaltime() -UFormat %s))
$postParams = #{command="returnBalances";nonce="$nonce"}
### Encode URL and convert to raw bytes
#$utf8enc = New-Object System.Text.UTF8Encoding
#$postParams_bytes = $utf8enc.GetBytes($postParams)
$hmacsha = New-Object System.Security.Cryptography.HMACSHA512
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secret)
$signature =
$hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($postParams))
$sign = [Convert]::ToBase64String($signature)
$headers2 = #{key="$key";sign="$sign"}
# $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
# $headers.Add("key",$key)
# $headers.Add("sign",$sign)
Invoke-WebRequest -Uri $uri -Method POST -Body $postParams -Headers $headers2 #| ConvertFrom-Json
BIG THANKS in advance for any help!!!
Well that was quite a ride; searched half the web, learned a lot and here is what I came up with. More functions may be adopted from https://pastebin.com/iuezwGRZ.
It would be great if you share your final script here also.
$PoloniexKey = "01234567-89ABCDEF-GHIJKLMN-OPQRSTUV"
$PoloniexSecret = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghij"
$TradingUri = 'https://poloniex.com/tradingApi'
$PublicUri = 'https://poloniex.com/public'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# source: https://stackoverflow.com/questions/40680882/trying-to-make-hmac-sha256-work-with-powershell-for-canvas-api
function Buffer($string) {
$c=#()
Foreach ($element in $string.toCharArray()) {$c+= [System.Convert]::ToSByte($element)}
return $c
}
# source: https://www.remkoweijnen.nl/blog/2013/04/05/convert-bin-to-hex-and-hex-to-bin-in-powershell/
function BinToHex {
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true)
]
[Byte[]]$Bin)
# assume pipeline input if we don't have an array (surely there must be a better way)
if ($bin.Length -eq 1) {$bin = #($input)}
$return = -join ($Bin | foreach { "{0:X2}" -f $_ })
return $return.ToLower()
}
# source: https://gist.github.com/jokecamp/2c1a67b8f277797ecdb3
function HmacSHA512($Message, $Secret) {
$HMACSHA512 = new-object System.Security.Cryptography.HMACSHA512
$HMACSHA512.key = Buffer -string $Secret
$StringToHash = [System.Text.Encoding]::UTF8.GetBytes($Message)
$HashByteArray = $HMACSHA512.ComputeHash($StringToHash)
$hash = BinToHex $HashByteArray
return $hash;
}
function Invoke-PoloniexRequest($Request, $Uri=$TradingUri) {
$Nonce = ([int64](Get-Date (get-date).touniversaltime() -UFormat %s))*10
$Request.Add('nonce', $Nonce)
$RequestArray=#()
$Request.GetEnumerator() | %{$RequestArray += ($_.Name,$_.Value -join '=')}
$Body = $RequestArray -join '&'
$Sign = HmacSHA512 -Message $Body -Secret $PoloniexSecret
$Headers = #{
Key = $PoloniexKey
Sign = $Sign
}
Invoke-WebRequest -Uri $Uri -Method POST -Body $Body -Headers $Headers | ConvertFrom-Json
}
function Get-PoloniexBalances {
$request = #{command="returnBalances"}
return Invoke-PoloniexRequest -Request $request
}
$Balances = Get-PoloniexBalances