Forgot to mention $user_id = 1
– where this is called it’s always passed as 1.
Also discovered that the url passed to jetpack with the oauth code appears to have been stuck in a loop urlencoding parts of itself over and over with the same variables added back every time. For example there are about 6 instances of redirect_uri in there urlencoded multiple times. I don’t want to post it because it contains tokens, but maybe somethings going wrong there.