Using Google oAuth on WP7
I trying out a small app for window开发者_JS百科s phone 7 that use Google tasks services, the problem is I'm not able to get pass to login.
I have read the all the steps given in documentation and following them
http://code.google.com/apis/accounts/docs/OAuth2.html#IA according to the document I need to make use of web control to login.Is there any way I can just expect username password and get the authentication token in background?
You can use ClientLogin to do things in the background, but it is being replaced (slowly) and doesn't work with all Google accounts (two-step) so I would suggest you stick with oAuth2, it definitely works.
How I do it is open a WebBrowser
control, making sure IsScriptEnabled="true"
then point it at
https://accounts.google.com/o/oauth2/auth?client_id=xxx&redirect_uri=https://www.mydomain.com/oauth2callback&scope=xxx&response_type=code
The really important part is the redirect url. You then wire up a Navigating
method for your WebBrowser
control to intercept the redirect to that url.
<phone:WebBrowser Name="webbrowser" Navigating="webbrowser_Navigating" IsScriptEnabled="true" />
private void webbrowser_Navigating(object sender, NavigatingEventArgs e) {
if (e.Uri.Host.Equals("www.mydomain.com")) {
e.Cancel = true;
HandleOAuthResponse(e.Uri.Query);
}
}
This will give you back the querystring google redirects with which has the code=xxx
that you then follow the rest of the docs and exchange it for a token that will last 30mins and a refresh token to keep the authentication active.
Have a look at this post it is about a twitter client but twitter uses OAuth too.
EDIT
I read more on this and it seems to be a problem because it is mandatory to use the webpage and have to user copy the access code to the app. It appears to be hard/impossible to scrape the Webbrowser I could not find any references to a real solution at the moment.
I think you can scrap the webBrowser using webBrowser.SaveToString() method.
You can actually scrape the browser URI by taking the e.Uri.ToString()
and use the replace method to remove items not needed e.g. thestring.Replace("http://fakeuri.com/code=","");
Only issue I have is with the Access Token.
精彩评论