PowerShell / .Net - Google API联系人修改:PUT请求401未经授权

问题描述 投票:0回答:1

我正在尝试使用.Net API修改Gmail帐户中的联系人,并将其加载到PowerShell中。我正在按照步骤described here(更新联系人,Doh!)

要更新联系人,请首先检索联系人条目,修改数据并将授权的PUT请求发送到联系人的编辑URL以及正文中已修改的联系人条目。

好的,得到它所以我成功使用此代码检索联系信息:

$Settings = New-Object Google.GData.Client.RequestSettings( "MyApp", $username , $password )
$Credentials = New-Object System.Net.NetworkCredential( $username, $password )

$Request = New-Object Google.Contacts.ContactsRequest( $Settings )
$Contacts = $Request.GetContacts()
$GoogleContact = $Contacts.Entries |? { $_.PrimaryEmail.Address -eq "[email protected]" }
$GoogleContact.Title

当然,我收到了来自谷歌的邮件消息,表明外部应用程序出现了问题,我更改了一个安全参数以允许此代码工作......并且它有效,代码正在提示我的Google联系人标题。

现在,我的问题: 我正在更改我的对象上的属性:

$GoogleContact.Title = "Mac Gyver"

我正在使用一个名为Execute-HTTPPostCommand的函数,稍加修改以添加Etag值,谷歌要求确保我不修改其他地方实际修改的条目:

function Execute-HTTPPostCommand() {
    param(
        [string] $TargetUrl = $null
        ,[string] $PostData = $null
        ,$Credentials
        ,$Etag
    )

    $ErrorActionPreference = "Stop"
    $global:webRequest = [System.Net.WebRequest]::Create($TargetUrl)
    $webRequest.Headers.Add("etag", $Etag )
    $webRequest.ContentType = "text/html"
    $PostStr = [System.Text.Encoding]::UTF8.GetBytes($PostData)
    $webrequest.ContentLength = $PostStr.Length
    $webRequest.ServicePoint.Expect100Continue = $false
    $webRequest.Credentials = $Credentials

    $webRequest.PreAuthenticate = $true
    $webRequest.Method = "PUT"

    $Global:requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($PostStr, 0,$PostStr.length)
    $requestStream.Close()

    [System.Net.WebResponse] $global:resp = $webRequest.GetResponse();
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();

    return $results;
}

并以这种方式调用它:

Execute-HTTPPostCommand -TargetUrl $GoogleContact.Id -PostData $GoogleContact -Credentials $Credentials -Etag $GoogleContact.ETag

Contact.ID值是谷歌更新联系人所需的URL,如下所示:https://www.google.com/m8/feeds/contacts/userEmail/full/ {contactId}

我收到错误401:未经授权。作为Windows系统管理员,我不熟悉Web服务PUT请求......我使用相同的凭据来读取数据并尝试更新数据。我错过了什么?

powershell gdata google-contacts google-shared-contacts google-profiles-api
1个回答
0
投票

好的,这很简单,我应该有RTFM ......

#Loading Google API
$Settings = New-Object Google.GData.Client.RequestSettings( "MyApp", $username , $password )
$Credentials = New-Object System.Net.NetworkCredential( $username, $password )

#Loading Contacts, and getting the one I want
$Request = New-Object Google.Contacts.ContactsRequest( $Settings )
$Contacts = $Request.GetContacts()
$GoogleContact = $Contacts.Entries |? { $_.PrimaryEmail.Address -eq "[email protected]" }

#Updating the fields
$GoogleContact.Name.FullName = "Mac Gyver"
$GoogleContact.Name.GivenName = "Mac"
$GoogleContact.Name.FamilyName = "Gyver"
$GoogleContact.Title = "Handyman Masterchief"

#Update
$MAJ = $ContactRequest.Update($GoogleContact)

它的工作原理就像.Net示例一样。

无需加载繁重的PUT请求,API可以完成他的工作。

很抱歉没时间了!

© www.soinside.com 2019 - 2024. All rights reserved.