从未真正使用过 OAuth,现在尝试实现它,我想从 google 和 facebook 获取访问 token 和配置文件数据。使用 Xamarin.Auth。
使用 Facebook 没有问题,我指定“http://www.facebook.com/connect/login_success.html”作为重定向网址,登录后它会返回到我之前的 Activity 。
但是使用 Google 就不太顺利了 - 找不到任何类似于 facebook 登录成功页面的内容,在某处找到了使用“https://www.googleapis.com/plus/v1/people/me ”的建议 - 将其添加到重定向网址白名单中,但是登录后我会得到 “Redirect_url_mismatch” native 应用程序:应用程序名称
根据他们的文档,我应该使用“my.package.name:”,我再次添加它来重定向网址白名单,尝试登录,这次是在登录之后在屏幕中,我进入第二个屏幕,我需要确认读取权限,之后我收到非常短的错误,例如“com.my.package:/?oauthparameterX=value1....”,并再次重定向到权限屏幕。
这是我完整的 OAuth2Authenticator:
var auth = new OAuth2Authenticator(
clientId: SocialIds.GooglePlusId,
clientSecret: SocialIds.GooglePlusSecret,
scope: OAuthUrl.GoogleScope,
authorizeUrl: new Uri(OAuthUrl.GoogleAuthorize),
redirectUrl: new Uri("https://www.googleapis.com/plus/v1/people/me"),
accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
getUsernameAsync: null);
auth.AllowCancel = false;
网址:
public static string GoogleAuthorize = "https://accounts.google.com/o/oauth2/auth";
public static string GoogleScope = "https://www.googleapis.com/auth/userinfo.email";
public static string GoogleRedirect = "https://www.googleapis.com/plus/v1/people/me";
public static string GoogleUserInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token={0}";
最佳答案
在上面列出的代码中,您没有给出重定向 uri,而是给出了 google api 的范围。重定向uri的目的是在授权后接收来自google api的响应。响应应该是代码。此响应代码用于访问access_token、refresh_token、id_token等。因此,您必须在项目端接收此代码。为此,使用了重定向 uri。 转到您的 Google 控制台,创建项目,添加凭据,然后您将被重定向到包含的页面,
您可以找到授权的重定向网址。提供 url,然后使用新的重定向 url 配置您的代码。在此之后一切都会好起来的。
关于android - Google OAuth Android 的重定向 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38438445/