.Net API を使用して Gmail アカウントの連絡先を変更し、powershell に読み込もうとしています。ここで説明されている手順に従っています(連絡先の更新、Doh !)
連絡先を更新するには、まず連絡先エントリを取得し、データを変更して、承認済みの PUT 要求を、変更された連絡先エントリを本文に含む連絡先の編集 URL に送信します。
OK、わかったので、次のコードを使用して連絡先情報を取得することに成功しました:
$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
。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 : unautorized が表示されます。Windows Sysadmin であるため、Web サービスの PUT 要求に慣れていません。同じ資格情報を使用してデータを読み取り、データを更新しようとしています。私は何が欠けていますか?