首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从移动应用调用rest服务器

从移动应用调用rest服务器
EN

Stack Overflow用户
提问于 2018-05-10 15:42:56
回答 3查看 464关注 0票数 1

https://lists.hyperledger.org/g/composer/message/91之后

我采用了卡罗琳·丘奇在我的IOS应用程序中描述的方法。再一次,我可以使用谷歌进行身份验证,但当使用POSTing时,仍然会出现401授权错误。

我已经在POST请求的http头中添加了withCredentials参数。

rest服务器是否在cookie中传回令牌?我没有收到任何从rest服务器返回的内容。

withCredentials从哪里获得凭据?

COMPOSER_PROVIDERS如下所示

代码语言:javascript
复制
COMPOSER_PROVIDERS='{
    "google": {
        "provider": "google",
        "module": "passport-google-oauth2",
        "clientID": "93505970627.apps.googleusercontent.com",
        "clientSecret": "",
        "authPath": "/auth/google",
        "callbackURL": "/auth/google/callback",
        "scope": "https://www.googleapis.com/auth/plus.login",
        "successRedirect": "myAuth://",
        "failureRedirect": "/"
    }
}'

successRedirect指向回我的应用程序。成功通过身份验证后,我返回应用程序。

EN

回答 3

Stack Overflow用户

发布于 2018-05-20 15:43:49

现在开始工作了。App首先向google进行身份验证,然后与rest服务器交换授权码。

需要更改Rest服务器COMPOSER_PROVIDERS以重新与应用程序关联。clientID为谷歌中的应用ID,callbackURL和successRedirect为reversed_clientID://

应用调用http://localhost:3000/auth/google/callback,参数为授权码。

此调用将失败,但会写回一个包含rest服务器所需的访问令牌的access_token cookie。

登录用户的用户id不会传回,当与google交换代码以换取令牌时,我们会得到一个包含登录用户详细信息的JWT。我们需要从rest服务器以及令牌中取回它。有没有办法弄到这个?

更改COMPOSER_PROVIDERS意味着Rest服务器的浏览器界面不再有效。

票数 1
EN

Stack Overflow用户

发布于 2018-07-24 18:31:50

代码语言:javascript
复制
func getRestToken(code: String) {        
    let tokenURL = "http://localhost:3000/auth/google/callback?code=" + code

    let url = URL(string:tokenURL);

    var request = URLRequest(url: url!);

    request.httpMethod = "GET";

    request.setValue("localhost:3000", forHTTPHeaderField: "Host");

    request.setValue("text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8", forHTTPHeaderField: "Accept");
    request.setValue("1", forHTTPHeaderField: "Upgrade-Insecure-Requests");

    request.httpShouldHandleCookies = true;
    request.httpShouldUsePipelining = true;

    let session = URLSession.init(configuration: .default);
    session.configuration.httpCookieAcceptPolicy = .always;
    session.configuration.httpShouldSetCookies=true;
    session.configuration.httpCookieStorage = HTTPCookieStorage.shared;

    let task = session.dataTask(with: request) { (data, response, error) in
            var authCookie: HTTPCookie? = nil;
            let sharedCookieStorage = HTTPCookieStorage.shared.cookies;

            // test for access_token
            for cookie in sharedCookieStorage! {
                if cookie.name == "access_token"
                {
                    print(“Received access token”)
                }
            }

            guard error == nil else {
                print("HTTP request failed \(error?.localizedDescription ?? "ERROR")")
                return
            }
            guard let response = response as? HTTPURLResponse else {
                print("Non-HTTP response")
                return
            }
            guard let data = data else {
                print("HTTP response data is empty")
                return
            }

            if response.statusCode != 200 {
                // server replied with an error
                let responseText: String? = String(data: data, encoding: String.Encoding.utf8)

                if response.statusCode == 401 {
                    // "401 Unauthorized" generally indicates there is an issue with the authorization
                    print("Error 401");
                } else {
                    print("HTTP: \(response.statusCode), Response: \(responseText ?? "RESPONSE_TEXT")")
                }
                return
            }

        }

    task.resume()

}
票数 1
EN

Stack Overflow用户

发布于 2018-05-10 21:34:35

你在你的Google OAUTH2配置中授权重定向URI了吗?

这决定了在用户完成授权流程后,API服务器将用户重定向到何处。该值必须与您的项目在redirect_uri控制台中列出的某个API值完全匹配。请注意,http或https方案、大小写和尾部斜杠('/')必须全部匹配。

这是一个Angular 5成功使用Angular 5, httpclient ignores set cookie in post的示例,特别是底部作用域的答案控制访问令牌所允许的一组资源和操作。在访问令牌请求期间,您的应用程序在scope参数中发送一个或多个值。

请参阅https://developers.google.com/identity/protocols/OAuth2

设置withCredentials选项是为了创建cookie,将身份验证令牌传递给REST服务器。

最后,这个资源可能会帮助你使用https://hackernoon.com/adding-oauth2-to-mobile-android-and-ios-clients-using-the-appauth-sdk-f8562f90ecff

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50267994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档