繁体   English   中英

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

[英]PowerShell/.Net - Google API Contacts Modification : PUT Request 401 Unauthorized

我正在尝试使用.Net API修改Gmail帐户中的联系人,并将其加载到Powershell中。 我正在按照此处描述的步骤进行操作(更新联系人,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 "john.doe@gmail.com" }
$GoogleContact.Title

当然,我从Google收到一封邮件,指示外部应用程序已泛滥,并且我更改了安全性参数以使此代码正常工作。并且它有效,该代码提示我的Google联系人标题。

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

$GoogleContact.Title = "Mac Gyver"

我正在使用一个名为Execute-HTTPPostCommand的函数Execute-HTTPPostCommand少许修改以添加Etag值,这是google所必需的,以确保我没有修改实际上在其他地方修改过的条目:

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值是Google更新联系人所需的URL,看起来像这样: https : //www.google.com/m8/feeds/contacts/userEmail/full/ {contactId}

我收到错误401:未经授权。 作为Windows Sysadmin,我不熟悉webservices PUT请求...我使用相同的凭据读取数据并尝试更新数据。 我想念什么?

好的,那很容易,我应该有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 "john.doe@gmail.com" }

#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即可完成工作。

对不起,浪费时间的家伙!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM