mkellerman / PSTwitterAPI

PSTwitterAPI - a PowerShell module for the Twitter API
https://mkellerman.github.io/Introducing_PSTwitterAPI/
45 stars 13 forks source link

Error thrown when POST-ing large payload #30

Open Windos opened 5 years ago

Windos commented 5 years ago

I'm trying to use Send-TwitterAccount_UpdateProfileBanner to update my account banner:

$Image = [Convert]::ToBase64String((Get-Content .\TwitterBanner.png -Encoding Byte))
Send-TwitterAccount_UpdateProfileBanner -banner $Image

Naturally, that Image string is HUGE, as I'm trying to send a 1500x500 image (the recommended size for one of these banners) that's 101 KB on disk.

When run, I get back

Get-OAuthParameters : Exception calling "EscapeDataString" with "1" argument(s): "Invalid URI: The Uri string is too long."
At C:\Users\joshuak\Documents\WindowsPowerShell\Modules\PSTwitterAPI\0.0.7\public\Invoke-TwitterAPI.ps1:26 char:24
+     $OAuthParameters = Get-OAuthParameters @OAuthParameters_Params
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-OAuthParameters

Invoke-RestMethod : Cannot bind parameter 'Method' to the target. Exception setting "Method": "Cannot convert null to type "Microsoft.PowerShell.Commands.WebRequestMethod" due to enumeration 
values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are "Default,Get,Head,Post,Put,Delete,Trace,Options,Merge,Patch"."
At C:\Users\joshuak\Documents\WindowsPowerShell\Modules\PSTwitterAPI\0.0.7\public\Invoke-TwitterAPI.ps1:34 char:23
+     Invoke-RestMethod @RestMethod_Params
+                       ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Looks like the entire image payload is being encoded as part of the URI... which if my understanding of that class is right is capped at 2048 characters? I've tried halving the size of the image, but it's still too long.

I think that shipping this data up as a JSON body on Invoke-RESTMethod would get it up there (I was playing around with this a little, but mucked up the authentication side of things lol)

Windos commented 5 years ago

Actually, scratch that comment about it being capped at 2048... been playing around and looks like it actually between 64,512 and 65,536 (I was testing in increments of 1024 and really didn't feel like getting more specific than that!)

mkellerman commented 5 years ago

Look at how I got the DirectMessages to work with a JSON body:

https://github.com/mkellerman/PSTwitterAPI/blob/master/PSTwitterAPI/public/Send-TwitterDirectMessages_EventsNew.ps1

Care to try your own implementation, and see if that fixes the file size limitation?

Windos commented 5 years ago

I stumbled upon that and have been playing around, unfortunately still no luck.

Initially I created a new private function ConvertTo-EscapedDataString which escapes the data in chunks, but then of course it finally gets to Invoke-RestMethod and it complains about the length of the final URI.

Then went down the JSON path, and the response back is:

Invoke-RestMethod : {"errors":[{"code":38,"message":"banner parameter is missing."}]}

The documentation for this endpoint and the DM one is subtly different and almost looks like this one won't take JSON.

I've started investigating (after some JS people mentioned it) using 'ContentType' = 'multipart/form-data'... but this is heading in to uncharted territory for me lol.

I'll keep playing.

Windos commented 5 years ago

Ok... don't think multipart is being accepted. Followed @markekraus example from when support for it was being added to PowerShell core.

Still getting parameter missing response.

mkellerman commented 4 years ago

@Windos have you tried with the latest version?

Windos commented 4 years ago

As in 0.0.7? Yeah, I tried it about 2 months ago. I haven't retried with PS7 and the rest method work that's happened in that space though.

On Fri, 28 Feb 2020 at 10:57, Marc R Kellerman notifications@github.com wrote:

@Windos https://github.com/Windos have you tried with the latest version?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mkellerman/PSTwitterAPI/issues/30?email_source=notifications&email_token=ABVCGCUEVW36JOGSKJIUD7LRFAZMJA5CNFSM4G3NDOS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENGDNNI#issuecomment-592197301, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVCGCSYKIJSKHDJCUWRZWDRFAZMJANCNFSM4G3NDOSQ .

mkellerman commented 4 years ago

I haven't played with sending images.. :/