0

I am getting the following error using Microsoft Azure Powershell to connect to Twitter API:

Exception calling "GetResponse" with "0" argument(s): The remote server returned an error: <401> Unauthorised." At line:1 char:1 + $response = $request.GetResponse() ; + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

The background is that I am starting with HDInsight and have followed the example at http://azure.microsoft.com/en-gb/documentation/articles/hdinsight-analyze-twitter-data/ which worked.

I am now redoing the first step (connecting to Twitter API) and trying to use a different field of the api ( Instead of using "track", I am using "follow" ) and the script is erroring.

The script I am running in Powershell is the following :

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

1 Answer 1

2

Use my PSTwitter module:

Powershell Twitter REST API 1.1 Module on TechNet Gallery…

now on github:

https://github.com/mkellerman/PSTwitterAPI

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.