微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Powershell将file upload到一个驱动器

我正在下面使用PowerShell将file upload到onedrive。 http://www.powershellmagazine.com/2014/04/07/using-the-windows-live-apis-from-powershell/

Invoke-RestMethod -Uri $Uri -Method Put -InFile $Path

其中是文件的完整path

$Path= "C:SkyDrive/ServerBackups/Webs/Webs/test.txt"

和$Uri = "https://login.live.com/oauth20_desktop.srf?code=XXXXXX

抛出(404)未find错误

“连接被远程接口拒绝的连接”与PDO连接到Firebird 3

如何在appveyor中安装GHC并将GHC放在path中?

如何运行firebird作为应用程序?

您无法将文件放入OAuth端点…对引用文章快速扫描表明您应该使用$ Uri值,看起来更像$ wlApiUri。 请参阅http://msdn.microsoft.com/en-US/library/dn659726.aspx ,了解这种操作的URL方案的描述。

Invoke-RestMethod使用的$ Uri的值是错误的。 您在脚本中使用的OAuth端点用于身份验证,而不是OneDrive操作。

关于如何将文件上传到OneDrive,这可以分为三个部分。

注册 OneDrive应用程序进行OAuth身份验证过程

调用OneDrive 身份验证API来获取访问令牌

使用访问令牌调用OneDrive API以上载文件

创建OneDrive应用程序后,如果应用程序是Web类型,则可以获取应用程序ID密钥重定向Uri 。 然后使用三个值下面的脚本。

# get authorize code Function Get-AuthroizeCode { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$ClientId,[Parameter(Mandatory=$true)][String]$RedirectURI ) # the login url $loginUrl = "https://login.live.com/oauth20_authorize.srf?client_id=$ClientId&scope=onedrive.readwrite offline_access&response_type=code&redirect_uri=$RedirectURI"; # open ie to do authentication $ie = New-Object -ComObject "InternetExplorer.Application" $ie.Navigate2($loginUrl) | Out-Null $ie.Visible = $True While($ie.Busy -Or -Not $ie.LocationURL.StartsWith($RedirectURI)) { Start-Sleep -Milliseconds 500 } # get authorizeCode $authorizeCode = $ie.LocationURL.SubString($ie.LocationURL.IndexOf("=") + 1).Trim(); $ie.Quit() | Out-Null RETURN $authorizeCode } # get access token and refresh token Function New-AccesstokenAndRefreshToken { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$ClientId,[Parameter(Mandatory=$true)][String]$RedirectURI,[Parameter(Mandatory=$true)][String]$SecrectKey ) # get authorize code firstly $AuthorizeCode = Get-AuthroizeCode -ClientId $ClientId -RedirectURI $RedirectURI $redeemURI = "https://login.live.com/oauth20_token.srf" $header = @{"Content-Type"="application/x-www-form-urlencoded"} $postBody = "client_id=$ClientId&redirect_uri=$RedirectURI&client_secret=$SecrectKey&code=$AuthorizeCode&grant_type=authorization_code" $response = Invoke-RestMethod -Headers $header -Method Post -Uri $redeemURI -Body $postBody $AccessRefreshToken = New-Object PSObject $AccessRefreshToken | Add-Member -Type NoteProperty -Name Accesstoken -Value $response.access_token $AccessRefreshToken | Add-Member -Type NoteProperty -Name RefreshToken -Value $response.refresh_token RETURN $AccessRefreshToken }

然后你得到有效的访问令牌,你可以为Invoke-RestMethod构建有效的头Invoke-RestMethod

# get autheticate header Function Get-AuthenticateHeader { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$Accesstoken ) RETURN @{"Authorization" = "bearer $Accesstoken"} }

最后,你可以通过上传rest api将头部传递给Invoke-RestMethod 。

有四个不同的OneDrive上传休息Api你可以打电话。

简单的项目上传

可恢复的项目上传

多部分项目上传

从Url上传

如果您的目标文件不是太大,意味着文件长度在100MB以内,我强烈建议使用简单的项目上传

对于大文件,通常会调用Resumable项目上传 API

这是一个很大的故事,您可以直接将此上传文件引用到OneDrive示例中以获得完整的脚本。

希望你能摆脱这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐