How can use cURL with Powershell to download from sharepoint using Microsoft Graph - powershell

I am trying to download some files from my SharePoint or OneDrive using the Microsoft graph API. I want to only use cURL (The C:\windows\systems32\curl not invoke-request) and powershell.
I tried using the documentation from Microsoft here:https://learn.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token
and here: https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes
However the syntax seems to be off and I have been having some trouble.
Here is my starting code (This assumes you have setup an appid in the azure portal and obtained and client_id,client_secret, and tenant id):
curl -d 'client_id'='ENTERCLIENTIDHERE' \
-d 'scope'='https://graph.microsoft.com/.default' \
-d 'client_secret'='ENTERCLIENTSECRETHERE' \
-d 'grant_type'='client_credentials' \
'https://login.microsoftonline.com/ENTERTANTANTHERE/oauth2/v2.0/token'

Here is code that will help you get a request from the Microsoft graph api using a token from your app id information, then it will generate a url for a specific file or folder of your choosing. You can also modify the code to just get yourself a token from the Microsoft graph API using only cURL and Powershell for windows.
#Generating the Token from the microsoft Graph API
$Result = C:\Windows\System32\curl -n -i -H "Host:login.microsoftonline.com" -H "Content-Type:application/x-www-form-urlencoded" -d "client_id=ENTERCLIENTIDHERE&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=ENTERCLIENTSECRETHERE&grant_type=client_credentials" "https://login.microsoftonline.com/ENTERTENANTIDHERE/oauth2/v2.0/token"
$x = $Result[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
$M = $x[16]
$k = $M -split ‘access_token":"’
$Resultk = $k[1]
$ResulO = $Resultk.Substring(0,$Resultk.Length-2)
#Seeing the token in powershell to verify it came up properly
Write-Output $ResulO
#Requesting a download url from your own sharepoint or onedrive
C:\Windows\System32\curl -H "Host:graph.microsoft.com" -H "Authorization:Bearer $ResulO" -H "X-Cookie:token=$ResulO" "https://graph.microsoft.com/v1.0/sites/ENTERSITENAMEORSITEIDHERE/drive/root:/ENTERFILEPATHORFILEHERE?select=id,#microsoft.graph.downloadUrl"

Related

Getting "not found" after authenticating when trying to initiate GitHub workflow via REST

I am trying to trigger the workflow_dispatch action for a GitHub workflow via REST but I am getting a "not found" error.
My question is similar to this one but the difference is that I am still getting the "not found" error even though the header indicates I am authenticated (the rate limit has increased to 5,000).
Here's my script:
#!/bin/bash
# https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
OWNER='myGithubOrganization'
REPO='myRepo'
WORKFLOW_ID='main.yml'
POST_URL="https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/dispatches"
echo "Calling $POST_URL"
GITHUB_PERSONAL_ACCESS_TOKEN=$(echo "$PLATFORM_VARIABLES" | base64 --decode | jq '.GITHUB_PERSONAL_ACCESS_TOKEN' --raw-output)
# -i to include headers.
curl \
-i \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_PERSONAL_ACCESS_TOKEN" \
$POST_URL \
-d '{"ref":"ref"}'
In the headers, I see the rate limit has increased to 5,000, so I know I am logged in.
The personal access token has the following permissions:
repo
workflow
admin:org_hook
The personal access token is for a machine user.
In the repo settings, under "Collaborators and teams", the machine user account has the "Read" role.
What more do I need to do to trigger the workflow?
The machine user needs to have write access, not read access.
This is true even if the workflow does something like run CI tests and does not write any code.

Trouble downloading ADYEN report

I'm using this script:
wget -O C:\FlairnetLab\output\x.csv --http-user='[My User]' --http-password='[Password]' --no-check-certificate https://ca-test.adyen.com/reports/download/MerchantAccount/FlairnetECOM/payments_accounting_report_2021_06_10.csv
But i get no file found response. But if i type the url in the browser using the same credential i can download the file.
Can some help me?
The problem is likely to be the encoding of the credentials (both Adyen-generated username and password include several special characters).
An option is to generate the base64-encoded string username: password with a command line (or with an online generator)
# example on Mac
$ echo -n '<username>:<password>' | openssl base64
cmVwb3J0.......5LX4=
then pass it in the Authorization header
# example with wget
wget --header "Authorization: Basic cmVwb3J0.......5LX4=" https://ca-test.adyen.com/reports/download/MerchantAccount/MyMerchantAccount/payments_accounting_report_2021_01_01.csv
# example with curl
curl -H "Authorization: Basic cmVwb3J0.......5LX4=" -X GET https://ca-test.adyen.com/reports/download/MerchantAccount/MyMerchantAccount/payments_accounting_report_2021_01_01.csv

Can I upload a package to Octopus from linux using curl without using NuGet or PowerShell?

We're building a package with Jenkins on a linux slave, and want to upload the package to an Octopus instance, but we don't have access to Nuget or Powershell on the linux box.
The Octopus examples say you can do this by
$wc = new-object System.Net.WebClient
$wc.UploadFile("http://octopus.example.com/api/packages/raw?apiKey=<Your API Key>", "YourApp.1.0.0.zip")
Can I do this with curl?
Yes, the following is the curl equivalent.
curl -X POST \
-H "X-Octopus-ApiKey: <Your API Key>" \
-F "file=#\"YourApp.1.0.0.zip\";filename=\"YourApp.1.0.0.zip\";type=application/zip" \
http://octopus.example.com/api/packages/raw?replace=true

ServiceM8 api email - how to relate to job diary

I can send an email from a ServiceM8 account through the ServiceM8 API 'message services' (http://developer.servicem8.com/docs/platform-services/message-services/), and read the resulting ServiceM8 message-id.
But I would like to relate that message to a specific job within ServiceM8, so that it will appear as an email item in that job's diary in the ServiceM8 web application. (Emails sent from within the ServiceM8 web application are related to the diary and appear there - my question is about how to do this from the API).
Worst case, I could create a new 'Note' containing the email text and add that to the job in the hope that it would show up in the diary in the web application as a note.
But I want to check there isn't an easier way since sending the email results in there already being a relatable message-id available within ServiceM8.
Thanks
Using the messaging services API, can't be done. Using the web API, you can do just that.
There's an authorisation code required, which is specific to your account and to this function, you only need to retrieve it once, and then you can integrate that specific URL into your code. It's contained within the ClientSidePlatform_PerSessionSetup URL.
Here is a script that will grab the E-mail URL specific to your login:
Syntax: ./getsm8emailurl.sh "email#address.com" "password"
#!/usr/bin/env bash
#getsm8emailurl.sh
#Create Basic auth
user="$1"
pass="$2"
pass="$(echo -n "${pass}" | md5sum | cut -f1 -d' ')"
auth="$(echo -n "${user}:${pass}" | base64)"
#Get Account specific e-mail url
email_url="https://go.servicem8.com/$(curl --compressed -s -L "https://go.servicem8.com/$(curl --compressed -s -L "https://go.servicem8.com/" -H "Authorization: Basic $auth" | grep -o 'ClientSidePlatform_PerSessionSetup.[^"]*' | grep -v "s_boolFailover")" -H "Authorization: Basic $auth" | grep -o "PluginEmailClient_SendEmail.[^']*")"
#Output base e-mail URL
echo "$email_url"
Once you have the email url, (will start with https://go.servicem8.com/PluginEmailClient_SendEmail and will end with the s_auth code), you can use it like any other rest endpoint.
Required Header Values:
Authorization (same as regular API)
Required Post Params:
s_form_values="guid-to-cc-subject-msg-job_id-attachedFiles-attachedContacts-strRegardingObjectUUID-strRegardingObject-boolAllowDirectReply"
(these have to stay just as they are)
s_auth="your_account_s_auth_code"
to="recipient#domain.com"
Optional Post Params:
subject="subject"
msg="html message body"
boolAllowDirectReply="true|false" (Can recipient reply directly to job diary)
strRegardingObject="job|company"
strRegardingObjectUUID="job|company uuid"
DEMO
#!/usr/bin/env bash
#sendemail.sh
#demo here using random auth codes and uuids
curl --compressed -s "https://go.servicem8.com/PluginEmailClient_SendEmail" \
-H "Authorization: Basic dGVzdHVzZXJAdGVzdGRvbWFpbi5jb206dGVzdHBhc3M=" \
-d s_form_values=guid-to-cc-subject-msg-job_id-attachedFiles-attachedContacts-strRegardingObjectUUID-strRegardingObject-boolAllowDirectReply \
-d s_auth="6akj209db12bikbs01hbobi3r0fws7j2" \
-d boolAllowDirectReply=true \
-d strRegardingObject=job \
-d strRegardingObjectUUID="512b3b2a-007e-431b-be23-4bd812f2aeaf" \
-d to="test#testdomain.com" \
-d subject="Job Diary E-mail" \
-d msg="hello"
Edit/Update/Disclaimer:
This information is for convenience and efficiency - memos, quick tasks, notifications, updates, etc. This isn't to be relied upon for critical business operations as it is undocumented, and since it does not process JS like a browser would, it could stop working if the inner workings of the service changed.

Facebook Application: Uploading multiple images in one post to wall

How to upload multiple images in one post via api?
Like here:
http://www.facebook.com/SocialCity?v=wall
I have managed to upload only one image via curl request.
curl
-F "access_token=token_here"
-F "message=message_here"
-F "picture=http://www.example.com/image.jpg"
https://graph.facebook.com/app_id_here/feed
Or it's not possible to post multiple images this way?
Anyone?
Thanks ;)
It's possible by now with the Open Graph however it seems to apply only to user generated fotos:
https://developers.facebook.com/docs/opengraph/usergeneratedphotos/
Multiple photo upload via one Graph API call is not supported. However you can create individual api calls and batch those in one request described here: https://developers.facebook.com/docs/reference/api/batch/
You can now publish multiple images in a single post to your feed or page:
For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false
curl -i -X POST \
-d "url=https%3A%2F%2Fwww.facebook.com%2Fimages%2Ffb_icon_325x325.png" \
-d "caption=test%20photo%20upload" \
-d "published=false" \
-d "access_token=<user_photos_token>" \
"https://graph.facebook.com/v2.6/me/photos"
You'll get an ID for each photo you upload like this:
{
"id": "10153677042736789"
}
Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo
curl -i -X POST \
-d "message=Testing%20multi-photo%20post!" \
-d "attached_media%5B0%5D=%7B%22media_fbid%22%3A%221002088839996%22%7D" \
-d "attached_media%5B1%5D=%7B%22media_fbid%22%3A%221002088840149%22%7D" \
-d "access_token=<publish_actions_token>" \
"https://graph.facebook.com/v2.6/me/feed"
Source: Publishing a multi-photo story