如何使用Google登录iOS从iOS客户端对Google Cloud Endpoints进行经过身份验证的呼叫?

时间:2022-08-22 11:25:02

I've implemented these instructions in my iOS app:

我在iOS应用中实现了这些说明:

https://developers.google.com/identity/sign-in/ios/sign-in

https://developers.google.com/identity/sign-in/ios/sign-in

And these ones in my Google Cloud Endpoints API:

以及我的Google Cloud Endpoints API中的这些内容:

https://cloud.google.com/appengine/docs/java/endpoints/consume_ios

https://cloud.google.com/appengine/docs/java/endpoints/consume_ios

except that of course I don't do the bit with the GTMOAuth2ViewControllerTouch.

除了当然我没有使用GTMOAuth2ViewControllerTouch。

The two do not marry up. The endpoints service in the iOS app needs an authorizer set on it, one that implements GTMFetcherAuthorizationProtocol. Where do I get this authorizer from the Google Sign-in callbacks?

两人不结婚。 iOS应用中的端点服务需要在其上设置授权器,实现GTMFetcherAuthorizationProtocol。我从哪里获得Google登录回调中的此授权人?

This callback on the AppDelegate gets passed a GIDGoogleUser instance, which has a GIDAuthentication instance, which has an accessToken field (String). That's what I want to pass to App Engine:

AppDelegate上的此回调传递给GIDGoogleUser实例,该实例具有GIDAuthentication实例,该实例具有accessToken字段(String)。这就是我想传递给App Engine的内容:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
    log.debug("Access token: \(user.authentication.accessToken)")
    // TODO: What do I do with this now?
}

Here's where the generated service needs the token. In my case the API is not very well named as just "Api", so the generated sources are GTLServiceApi, GTLQueryApi, etc.

这是生成的服务需要令牌的地方。在我的情况下,API没有很好地命名为“Api”,因此生成的源是GTLServiceApi,GTLQueryApi等。

private func executeQuery(query: GTLQueryApi, completionBlock: (object: AnyObject!, error: NSError!) -> Void) -> Void {
    let service = GTLServiceApi()
    service.retryEnabled = true

    // Whatever we set here should implement GTMFetcherAuthorizationProtocol. Where do we get one of those from the Google Sign-in SDK?
    // service.authorizer = TODO

    service.executeQuery(query, completionHandler: {(ticket, object, error) -> Void in
        completionBlock(object: object, error: error)
    })
}

2 个解决方案

#1


1  

(I've been tracking this issue for months, and finally figured it out...)

(我几个月来一直在跟踪这个问题,最后想出来......)

Once you are signed in, the GIDGoogleUser object returned has the authorizer attached as: user.authentication.fetcherAuthorizer

登录后,返回的GIDGoogleUser对象将授权程序附加为:user.authentication.fetcherAuthorizer

This object can be used as the authorizer for the endpoints service and everything seems to work!

这个对象可以用作端点服务的授权者,一切似乎都有效!

That said, if you want to make your app more closely follow the approach laid out at https://cloud.google.com/appengine/docs/java/endpoints/consume_ios, you might instead use GTMAppAuth, which seems to be the more similarly designed replacement for the GTMOAuth2 approach in that example.

也就是说,如果你想让你的应用更加贴近https://cloud.google.com/appengine/docs/java/endpoints/consume_ios所采用的方法,你可能会改用GTMAppAuth,这似乎更多在该示例中,类似地设计了GTMOAuth2方法的替代品。

#2


0  

In the GIDSignIn Delegate method "didSignIn for" just add

在GIDSignIn Delegate方法中,“didSignIn for”只是添加

let authorizer = user.authentication.fetcherAuthorizer()
self.service.authorizer = authorizer

(Swift 4)

(斯威夫特4)

#1


1  

(I've been tracking this issue for months, and finally figured it out...)

(我几个月来一直在跟踪这个问题,最后想出来......)

Once you are signed in, the GIDGoogleUser object returned has the authorizer attached as: user.authentication.fetcherAuthorizer

登录后,返回的GIDGoogleUser对象将授权程序附加为:user.authentication.fetcherAuthorizer

This object can be used as the authorizer for the endpoints service and everything seems to work!

这个对象可以用作端点服务的授权者,一切似乎都有效!

That said, if you want to make your app more closely follow the approach laid out at https://cloud.google.com/appengine/docs/java/endpoints/consume_ios, you might instead use GTMAppAuth, which seems to be the more similarly designed replacement for the GTMOAuth2 approach in that example.

也就是说,如果你想让你的应用更加贴近https://cloud.google.com/appengine/docs/java/endpoints/consume_ios所采用的方法,你可能会改用GTMAppAuth,这似乎更多在该示例中,类似地设计了GTMOAuth2方法的替代品。

#2


0  

In the GIDSignIn Delegate method "didSignIn for" just add

在GIDSignIn Delegate方法中,“didSignIn for”只是添加

let authorizer = user.authentication.fetcherAuthorizer()
self.service.authorizer = authorizer

(Swift 4)

(斯威夫特4)