0

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

4
  • The errors you posted don't match the code you posted. Commented Dec 29, 2015 at 21:05
  • @AnsgarWiechers, sorry, I edited, if you know the solution, please let me know. Thank you so much. Commented Dec 29, 2015 at 22:18
  • @Kazu, the error tells us $form.Fields["Apple ID"] = "myusername"; fails, as if value of $form was $null. This would happen if the web request to $loginUrl fails. What is $r.StatusCode equal to? You want it to be 200. Commented Dec 29, 2015 at 22:55
  • @AnsgarWiechers, Thank you for trhe reponse, my password and username is definitely right, so I am confused about how I can fix this problem. where and How should I change to make it Commented Dec 29, 2015 at 23:08

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much for response, I am having still same error, do you know how to fix this error?
You're still missing the /wo at the end of your loginUrl
Yes, It works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Thank you so much!!!!
For your second issue, check the iTunes Connect API's. Also check your web session variable and make sure that some form of authentication is stored within it.
I did a quick search of for iTunes Connect API's online and found the github reference, that I included in my first post.
|

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.