Javascript 使用谷歌&x2B;登录以验证对应用程序引擎后端的调用
因此,我遵循QuickStart应用程序(Web),添加了一个Google+登录按钮,如其文档中所述: 我让那个部分工作得很好,很好,在本地以及部署到应用程序引擎 现在,我想利用Google+登录提供的机会,以与此处所述类似的方式对后端端点进行身份验证调用: 这可能吗?我想知道Google+身份验证会在这个流程中“合并”哪一部分,这样我就不必调用它了Javascript 使用谷歌&x2B;登录以验证对应用程序引擎后端的调用,javascript,google-app-engine,google-plus,google-cloud-endpoints,Javascript,Google App Engine,Google Plus,Google Cloud Endpoints,因此,我遵循QuickStart应用程序(Web),添加了一个Google+登录按钮,如其文档中所述: 我让那个部分工作得很好,很好,在本地以及部署到应用程序引擎 现在,我想利用Google+登录提供的机会,以与此处所述类似的方式对后端端点进行身份验证调用: 这可能吗?我想知道Google+身份验证会在这个流程中“合并”哪一部分,这样我就不必调用它了 gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: mode
gapi.auth.authorize({client_id: CLIENT_ID,
scope: SCOPES, immediate: mode},
authorizeCallback);
再说一次,因为我的用户已经使用Google+登录了
另外,在我的实验中,有两个观察结果可以使这两种方法一起工作:
1.)我必须“导入”2个外观类似的脚本,即:
<script src="https://apis.google.com/js/client.js?onload=init">
对于OAuth2“样式”,以及
<script src="https://apis.google.com/js/client:plusone.js"></script>
()
或
()
谷歌+
2.)它们在加载时都有回调,我不知道它们如何在某个地方“合并”
3.)无论如何,我试着同时运行它们,并且在某个时候调用和端点方法。用户参数为空
我想知道这是否真的可行,或者我只是在浪费时间?使用Google+登录用户可能会为应用程序提供更多功能
对此有什么想法吗?请看。此应用程序向您展示了如何使用RESTful客户端在Google app Engine上构建应用程序。我通过查找并检查使用Google+登录方法连接到应用程序的Google云的,解决了此问题,然后如示例所示,调用需要授权的方法 ,使用Tic-Tac-Toe示例让我很不舒服,因为它使用的是“纯OAuth”,而Github中的Tic-Tac-Toe示例可能已更新为使用Google+signin 不管怎么说,我发现它实际上非常流畅和简单。当Google+登录回调成功执行身份验证时,您现在可以加载api并从那里开始。比如说,
function signinCallback(authResult) {
if (authResult['status']['signed_in']) {
// Update the app to reflect a signed in user
// Hide the sign-in button now that the user is authorized, for example:
document.getElementById('signinButton').setAttribute('style', 'display: none');
// we are now authenticated at this point, and we can load our
// Google Cloud Endpoints
gapi.client.load(apiName, apiVersion, apiCallback, apiRoot);
} else {
// Update the app to reflect a signed out user
// Possible error values:
// "user_signed_out" - User is signed-out
// "access_denied" - User denied access to your app
// "immediate_failed" - Could not automatically log in the user
console.log('Sign-in state: ' + authResult['error']);
}
}
之后,当调用我的GoogleCloudEndpointAPI方法时,它现在接收一个用户对象,而不是像以前一样为null
/**
* This inserts a new <code>Payload</code> object.
*
* @param payload The object to be added.
* @return The object to be added.
*/
@ApiMethod(name = "insertPayload")
public MapPackage insertPayload(Payload payload, User user) throws OAuthRequestException {
// Implement this function
LOG.info("Calling insertPayload method");
if (user == null) {
LOG.warning("User is null!");
} else {
//YEHEEY!!!!
LOG.warning("User ID:" + user.getUserId());
LOG.warning("User AuthDomain:" + user.getAuthDomain());
LOG.warning("User nickname:" +user.getNickname());
LOG.warning("User fedId:" +user.getFederatedIdentity());
LOG.warning("User email:" +user.getEmail());
}
return payload;
}
另一件需要注意的事情是,使它工作的最小范围是
email
您应该在后端API签名以及HTML+Javascript客户端中引用相同的作用域。例如,在后端API注释中:
import javax.inject.Named;
/**
* An endpoint class we are exposing
*/
@Api(name = "yourservicename",
version = "v1",
scopes = { https://www.googleapis.com/auth/plus.login },
clientIds = {Constants.WEB_CLIENT_ID, Constants.DEBUG_ANDROID_CLIENT_ID, Constants.PROD_ANDROID_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE},
namespace = @ApiNamespace(ownerDomain = "backend.yourappname.yourcompany.com",
ownerName = "backend.yourappname.yourcompany.com",
packagePath = ""))
HTML标记:
<span id="signinButton">
<span
class="g-signin"
data-callback="signinCallback"
data-clientid="YOUR_CLIENT_ID"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schema.org/AddAction"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
</span>
希望这有帮助
<span id="signinButton">
<span
class="g-signin"
data-callback="signinCallback"
data-clientid="YOUR_CLIENT_ID"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schema.org/AddAction"
data-scope="https://www.googleapis.com/auth/plus.login">
</span>
</span>