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
- You start off by GETting the login url which is something like https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/directToSignIn.
- 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
- Along with the cookie you get the login form that you should POST to the Uri denoted in the action attribute.
- 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).
- 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).
- 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).
- So a complete login UrL where you POST your data to could look like
- 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
- Now you are logged in and all future requests will be based at the above address.
Verify AppleID Email
- 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.”
- 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; $Message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($exService, $Mail.UniqueId) $Message.DateTimeReceived; $Message.Subject; $Message.ToRecipients.Address; # Get mail and extract VerificationUrl $fReturn = $Message.Body.Text -match '\"(https://id.apple.com/[^\"]+)\"'; $fReturn; $VerificationUrl = $Matches; $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/126.96.36.199.15.EditNameAndEmailAddress.1">https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/283/wo/R7ESZtr5DaEn3cI8rrIIXx/188.8.131.52.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.184.108.40.206.1.3=Edgar& 0.85.17.15.EditNameAndEmailAddress.220.127.116.11.1.7.3=& 0.85.17.15.EditNameAndEmailAddress.18.104.22.168.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.22.214.171.124=username-new%40example.com& 0.85.17.15.EditNameAndEmailAddress.126.96.36.199.1=username%40example.com& 0.85.17.15.EditNameAndEmailAddress.188.8.131.52=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.184.108.40.206=& 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.