예외가 "0"인수 (들) "하는 GetResponse를"호출을 : 원격을 서버 에러 반환 < 401> 무단을 "광고에서 1 개 문자 :. 1 개 + $ 응답 = $ request.GetResponse(); + CategoryInfo : NotSpecified (:) [] MethodInvocationException + FullyQualifiedErrorId : WebException이
배경은 내가 HDInsight로 시작하고에서 예제를 따라했습니다. http://azure.microsoft.com/en-gb/documentation/articles/hdinsight-analyze-twitter-data/.
이제 Twitter의 API에 연결하는 첫 번째 단계를 다시 수행하고 "track"을 사용하는 대신 "follow"를 사용하는 대신 스크립트의 다른 필드를 사용하려고합니다.
내가 PowerShell에서 실행하고 스크립트는 다음
Write-Host "Set variables ..." -ForegroundColor Green
$storageAccountName = "mystorageaccountname"
$containerName = "containername"
$destBlobName = "tutorials/twitter/data/tweets.txt"
$followString = "1920771606"
$follow = [System.Uri]::EscapeDataString($followString);
$lineMax = 10 #about 3 minutes every 100 lines
$oauth_consumer_key = "mykey";
$oauth_consumer_secret = "mysecret";
$oauth_token = "myoauthtoken";
$oauth_token_secret = "my_oauth_secret";
Write-Host "Define the Azure storage connection string ..." -ForegroundColor Green
$storageAccountKey = get-azurestoragekey $storageAccountName | %{$_.Primary}
$storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$storageAccountKey"
Write-Host "Create block blob object ..." -ForegroundColor Green
$storageAccount = [Microsoft.WindowsAzure.Storage.CloudStorageAccount]::Parse($storageConnectionString)
$storageClient = $storageAccount.CreateCloudBlobClient();
$storageContainer = $storageClient.GetContainerReference($containerName)
$destBlob = $storageContainer.GetBlockBlobReference($destBlobName)
Write-Host "Define a MemoryStream and a StreamWriter for writing ..." -ForegroundColor Green
$memStream = New-Object System.IO.MemoryStream
$writeStream = New-Object System.IO.StreamWriter $memStream
Write-Host "Format oauth strings ..." -ForegroundColor Green
$oauth_nonce = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([System.DateTime]::Now.Ticks.ToString()));
$ts = [System.DateTime]::UtcNow - [System.DateTime]::ParseExact("01/01/1970", "dd/MM/yyyy", $null)
$oauth_timestamp = [System.Convert]::ToInt64($ts.TotalSeconds).ToString();
$signature = "POST&";
$signature += [System.Uri]::EscapeDataString("https://stream.twitter.com/1.1/statuses/filter.json") + "&";
$signature += [System.Uri]::EscapeDataString("oauth_consumer_key=" + $oauth_consumer_key + "&");
$signature += [System.Uri]::EscapeDataString("oauth_nonce=" + $oauth_nonce + "&");
$signature += [System.Uri]::EscapeDataString("oauth_signature_method=HMAC-SHA1&");
$signature += [System.Uri]::EscapeDataString("oauth_timestamp=" + $oauth_timestamp + "&");
$signature += [System.Uri]::EscapeDataString("oauth_token=" + $oauth_token + "&");
$signature += [System.Uri]::EscapeDataString("oauth_version=1.0&");
$signature += [System.Uri]::EscapeDataString("follow=" + $follow);
$signature_key = [System.Uri]::EscapeDataString($oauth_consumer_secret) + "&" + [System.Uri]::EscapeDataString($oauth_token_secret);
$hmacsha1 = new-object System.Security.Cryptography.HMACSHA1;
$hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($signature_key);
$oauth_signature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($signature)));
$oauth_authorization = 'OAuth ';
$oauth_authorization += 'oauth_consumer_key="' + [System.Uri]::EscapeDataString($oauth_consumer_key) + '",';
$oauth_authorization += 'oauth_nonce="' + [System.Uri]::EscapeDataString($oauth_nonce) + '",';
$oauth_authorization += 'oauth_signature="' + [System.Uri]::EscapeDataString($oauth_signature) + '",';
$oauth_authorization += 'oauth_signature_method="HMAC-SHA1",'
$oauth_authorization += 'oauth_timestamp="' + [System.Uri]::EscapeDataString($oauth_timestamp) + '",'
$oauth_authorization += 'oauth_token="' + [System.Uri]::EscapeDataString($oauth_token) + '",';
$oauth_authorization += 'oauth_version="1.0"';
$post_body = [System.Text.Encoding]::ASCII.GetBytes("follow=" + $follow);
Write-Host "Create HTTP web request ..." -ForegroundColor Green
[System.Net.HttpWebRequest] $request = [System.Net.WebRequest]::Create("https://stream.twitter.com/1.1/statuses/filter.json");
$request.Method = "POST";
$request.Headers.Add("Authorization", $oauth_authorization);
$request.ContentType = "application/x-www-form-urlencoded";
$body = $request.GetRequestStream();
$body.write($post_body, 0, $post_body.length);
$body.flush();
$body.close();
$response = $request.GetResponse() ;
Write-Host "Start stream reading ..." -ForegroundColor Green
$sReader = New-Object System.IO.StreamReader($response.GetResponseStream())
$inrec = $sReader.ReadLine()
$count = 0
while (($inrec -ne $null) -and ($count -le $lineMax))
{
if ($inrec -ne "")
{
Write-Host $count.ToString() ", " -NoNewline -ForegroundColor Green
if ($count%10 -eq 0){
write-host " --- " -NoNewline
Get-Date -Format hh:mm:sstt
}
$writeStream.WriteLine($inrec)
$count ++
}
$inrec=$sReader.ReadLine()
}
Write-Host "Write to the destination blob ..." -ForegroundColor Green
$writeStream.Flush()
$memStream.Seek(0, "Begin")
$destBlob.UploadFromStream($memStream)
$sReader.close()
Write-Host "Complete!" -ForegroundColor Green