Kazu
Kazu

Reputation: 137

Invoke-WebRequest, hashtable, Cannot index into a null array

I'm trying to use Invoke-WebRequest, but I am getting an error.

I assume that the hash table is causing issues. How can I fix this error?

$loginBase = "https://itunesconnect.apple.com";
$loginUrl = "$($loginBase)/WebObjects/iTunesConnect.woa";

# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;

# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc. 
$ws;

# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable. 
$form = $r.Forms[0];

# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields

# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "[email protected]"
#$form.Fields["pass"] = "P@ssw0rd"

# I changed like this, following above example.
# Set the login data 
$form.Fields["Apple ID"] = "[email protected]";
$form.Fields["Password"]  = "myP2ssword";

# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields

$r.StatusDescription

I am getting this error.

Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:26 char:1
+ $form.Fields["Apple ID"] = "[email protected]";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:27 char:1
+ $form.Fields["Password"]  = "myP2ssword";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Invoke-WebRequest : Cannot bind parameter 'Method' to the target. Exception
setting "Method": "Object reference not set to an instance of an object."
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:30 char:79
+ $r=Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $f ...
+                                                                               ~~
    + CategoryInfo          : WriteError: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Invoke-WebRequest : {"data":null,"messages":{"warn":null,"info":null,"error":["Unauthorized access"]},"statusCode":"ERROR"}
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:35 char:13
+ $response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws;
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:36 char:27
+ $json = $response.Content|ConvertFrom-Json
+                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

So I edited like this:

$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa";

# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;

# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc. 
$ws;

# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable. 
$form = $r.Forms[0];

# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields

# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "[email protected]"
#$form.Fields["pass"] = "P@ssw0rd"

# I changed like this, following above example.
# Set the login data 
$form.Fields["Apple ID"] = "myusername";
$form.Fields["Password"]  = "mypassword";

# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields

$r.StatusDescription

The error is still same.

Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:27 char:1
+ $form.Fields["Apple ID"] = "myusername";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:28 char:1
+ $form.Fields["Password"]  = "mypassword";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Invoke-WebRequest : Cannot bind parameter 'Method' to the target. Exception
setting "Method": "Object reference not set to an instance of an object."
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:31 char:81
+ ... on $ws -Method $form.Method -Body $form.Fields
+                    ~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

This is re-re edited. Thank you so much for the help.

$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa/wo";

# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;

# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc. 
$ws;

# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable. 
$form = $r.Forms[0];

# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields

# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "[email protected]"
#$form.Fields["pass"] = "P@ssw0rd"

# I changed like this, following above example.
# Set the login data 
$form.Fields["Apple ID"] = "myusername";
$form.Fields["Password"]  = "mypassword";

# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields

$r.StatusDescription

# above this is totally working, under this I am having issue

$jsonSummaryUrl = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/manageyourapps/summary";

$response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws;
$json = $response.Content|ConvertFrom-Json

# Show me what apps I have

$json.data.summaries

the error is

Invoke-WebRequest : {"data":null,"messages":{"warn":null,"error":["Unauthorized access"],"info":null},"statusCode":"ERROR"} At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:36 char:13 + $response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null. At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:37 char:27 + $json = $response.Content|ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

Upvotes: 0

Views: 2396

Answers (1)

bsumner
bsumner

Reputation: 68

Your $loginUrl variable is off, try the following:

$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa/wo";

Source: https://github.com/fastlane/itc-api-docs

Update: You're missing the /wo at the end of the loginUrl try:

$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa/wo";

# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;

# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc. 
$ws;

# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable. 
$form = $r.Forms[0];

# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields

# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "[email protected]"
#$form.Fields["pass"] = "P@ssw0rd"

# I changed like this, following above example.
# Set the login data 
$form.Fields["Apple ID"] = "myusername";
$form.Fields["Password"]  = "mypassword";

# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields

$r.StatusDescription

Upvotes: 3

Related Questions