Communication with Apple iTunes Store and WebSite

Recently I had the task to automate a few things regarding AppleIDs. One of things was to create, change and verify AppleIDs. Having no tools (apart from Apples inofficial buggy AppleScript screen-click-automation scripts) I had a look at the HTTP communication between iTunes/the web client and the iTunes Store and AppleID servers.

The login to AppleID is rather straightforward by posting username and password via a login form. However note, that input controls in form have often different names and ids. In addition the names in forms may differ depending on the web client you use to connect to the AppleID servers.

Login to AppleID

  1. You start off by GETting the login url which is something like https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/directToSignIn.
  2. There you get your first session specific cookie ‘mymgrap_mycpr’ and some locale specific stuff. The richer the browser the more cookies you get.
    The cookie looks like to concatenated GUIDs without any hyphens, eg bc214ee621c546d3a1869ced550480d66f91e475600c477f8c8c0012440f7f18
  3. Along with the cookie you get the login form that you should POST to the Uri denoted in the action attribute.
  4. Within the form you get another session specific variable ‘wosid’ that you have to send in your form and that you will find in all your subsequent requests to the AppleID servers. That variable is relatively short (eg R7ESZtr5DaEn3cI8rrIIXx).
  5. Another interesting point is that at this point you seem to be redirected to a specific AppleID server instance which is also encapsulated in action attribute of the form. It has the form of a number (2-3 digits, eg 283).
  6. In addition you are send to a page specific address that comes in a form of digit-dot-notation. you will find these declarations later on all over their web pages (eg 0.0.85.17.1 for logging in).
  7. So a complete login UrL where you POST your data to could look like
    https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/283/wo/R7ESZtr5DaEn3cI8rrIIXx/0.0.85.17.1
    0.85.17.1.1=&0.85.17.1.7.0.1=&theAccountName=username%40example.com&theAccountPW=P%40ssw0rd&wosid=R7ESZtr5DaEn3cI8rrIIXx&signInHyperLink=Sign+in&Nojive=&theTypeValue=
  8. If everything works fine you are you are being forwarded to a url besides being awarded with a few cookies (actually the only cookie you really need is the ‘mymgrap_mypcr’ cookie that you already have).
    The location header looks something like this: /cgi-bin/WebObjects/MyAppleId.woa/283/wo/R7ESZtr5DaEn3cI8rrIIXx/2.0
  9. Now you are logged in and all future requests will be based at the above address.

Verify AppleID Email

  1. To verify the AppleID Email (the actual email address, not the recovery address) you have to look for a mail with the following subject: “Please verify the contact email address for your Apple ID.”
  2. Then extract the confirmation link with the following (.NET) regular expression: ‘\”(https://id.apple.com/%5B^\”]+)\”‘.

You could use the following code to get the latest confirmation email from an Office365 or Exchange mailbox:

#Connect to EWS first
Connect-OSCEXOWebService -Credential $cred
# Search for mail
$aMail = Search-OSCEXOEmailMessage -WellKnownFolderName Inbox -Subject "Please verify the contact email address for your Apple ID.";
$aMail.Count;
$Mail = $aMail[0];
$Message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($exService, $Mail.UniqueId)
$Message.DateTimeReceived;
$Message.Subject;
$Message.ToRecipients[0].Address;
# Get mail and extract VerificationUrl
$fReturn = $Message.Body.Text -match '\"(https://id.apple.com/[^\"]+)\"';
$fReturn;
$VerificationUrl = $Matches[1];
$VerificationUrl

Change Email Address

Changing an email address is easy too, but looks a little bit noisy and redundant:

POST <a href="https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/283/wo/R7ESZtr5DaEn3cI8rrIIXx/3.0.85.17.15.EditNameAndEmailAddress.1">https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/283/wo/R7ESZtr5DaEn3cI8rrIIXx/3.0.85.17.15.EditNameAndEmailAddress.1</a> HTTP/1.1
Host: appleid.apple.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/71/wo/R7ESZtr5DaEn3cI8rrIIXx/2.0
Cookie: ISTLocale=ZW5fVVM=; mymgrap_mycpr=bc214ee621c546d3a1869ced550480d66f91e475600c477f8c8c0012440f7f18
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 963

0.85.17.15.EditNameAndEmailAddress.1.1=&
0.85.17.15.EditNameAndEmailAddress.1.11.3.5.1.3=Edgar&
0.85.17.15.EditNameAndEmailAddress.1.11.3.5.1.7.3=&
0.85.17.15.EditNameAndEmailAddress.1.11.3.5.1.11=Schnittenfittich&
0.85.17.15.EditNameAndEmailAddress.1.11.5=false&
0.85.17.15.EditNameAndEmailAddress.1.11.7=Edgar&
0.85.17.15.EditNameAndEmailAddress.1.11.9=Schnittenfittich&
0.85.17.15.EditNameAndEmailAddress.1.11.11=&
0.85.17.15.EditNameAndEmailAddress.1.11.13=&
0.85.17.15.EditNameAndEmailAddress.1.11.15=&
0.85.17.15.EditNameAndEmailAddress.1.13=0&
0.85.17.15.EditNameAndEmailAddress.1.19.21.1=username-new%40example.com&
0.85.17.15.EditNameAndEmailAddress.1.19.21.7.1=username%40example.com&
0.85.17.15.EditNameAndEmailAddress.1.19.21.9=false&
0.85.17.15.EditNameAndEmailAddress.1.19.25=username-new%40example.com&
0.85.17.15.EditNameAndEmailAddress.1.27.5=&
0.85.17.15.EditNameAndEmailAddress.1.27.9.1=&
0.85.17.15.EditNameAndEmailAddress.1.33=SaveAppleIdAndEmail&
wosid=R7ESZtr5DaEn3cI8rrIIXx

Maybe this information comes in helpful when you need it. At least for me it helped me to create a 850 AppleID accounts, assigned software and changed email addresses – certainly all done with PowerShell … And in case you are interested I used a mixture of Invoke-WebRequest, [System.Net.WebClient] and [System.Net.WebRequest] to make the calls. And without the help of Fiddler I could not have done this either.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: