iOS 中的 OAuth 2.0 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18192957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OAuth 2.0 integration in iOS
提问by Supertecnoboff
I have been trying for ages now to get OAuth 2.0 integration in my iPhone application.
我一直在尝试将 OAuth 2.0 集成到我的 iPhone 应用程序中。
I have searched and searched for libraries, tutorials, etc... But they have all lead me to a dead end. The main problem I have is that they either have deprecated code, or they just don't work or they have some documentation but its really hard to follow (for me anyway...).
我搜索并搜索了图书馆、教程等……但它们都把我带到了死胡同。我遇到的主要问题是,他们要么弃用了代码,要么根本不起作用,或者他们有一些文档,但确实很难遵循(无论如何对我来说......)。
The best OAuth2 library I could find for Xcode is this one: https://github.com/nxtbgthng/OAuth2Client
我能为 Xcode 找到的最好的 OAuth2 库是这个:https: //github.com/nxtbgthng/OAuth2Client
But the main problem with that one is it doesn't seem to do anything... I have followed all the documentation and instructions that came with it, but after building and running, it doesn't seem to authenticate....
但是那个的主要问题是它似乎没有做任何事情......我已经遵循了它附带的所有文档和说明,但是在构建和运行之后,它似乎没有进行身份验证......
So I guess my main question is: does anyone know of any good and up to date OAuth 2.0 tutorials for Xcode or any libraries for such a thing?
所以我想我的主要问题是:有没有人知道 Xcode 的任何好的和最新的 OAuth 2.0 教程或任何类似的库?
Because I am really struggling at the moment.... :(
因为我现在真的很挣扎....:(
Thanks for your time, Dan.
谢谢你的时间,丹。
UPDATE 2: Here is my code (App Id and secret removed for security):
更新 2:这是我的代码(为安全起见,删除了应用程序 ID 和机密):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
oauthClient = [[LROAuth2Client alloc]
initWithClientID:@"MY_CLIENT_ID"
secret:@"MY_APP_SECRET"
redirectURL:[NSURL URLWithString:@"app://instagram-callback/?code="]];
oauthClient.delegate = self;
oauthClient.userURL = [NSURL URLWithString:@"https://api.instagram.com/oauth/authorize/?client_id=ab6dc96859bf43b3a488199ec72d9964&redirect_uri=app://instagram-callback/?code=&response_type=code"];
oauthClient.tokenURL = [NSURL URLWithString:@"https://api.instagram.com/oauth/access_token/"];
[oauthClient authorizeUsingWebView:myWebView];
}
- (void)oauthClientDidReceiveAccessToken:(LROAuth2Client *)client;
{
LROAuth2AccessToken *token = client.accessToken;
[NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
}
- (void)checkAccessTokenForExpiry:(LROAuth2AccessToken *)accessToken;
{
if ([accessToken hasExpired]) {
[oauthClient refreshAccessToken:accessToken];
}
}
- (void)oauthClientDidRefreshAccessToken:(LROAuth2Client *)client;
{
LROAuth2AccessToken *token = client.accessToken;
[NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
}
回答by dOM
In almost all projects I have used AFNetworkingbecause it's very powerful -why re-invent the wheel every time :)
在几乎所有项目中,我都使用了AFNetworking,因为它非常强大——为什么每次都要重新发明轮子:)
Furthermore, it also has an OAuth2Managerwhich is quite easy to implement and works rock-solid.
此外,它还有一个OAuth2Manager,它很容易实现并且工作稳定。
回答by aramusss
In Instagram documentationsays that there are two ways to authenticate. One Explicit (for server-side auth) and one Implicit, for auth in a client (without server).
在Instagram 文档中说有两种方法可以进行身份验证。一个显式(用于服务器端身份验证)和一个隐式,用于客户端(无服务器)中的身份验证。
You are using the Explicit one inside the app, try changing the userURL
to https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token
and the tokenURL
to http://your-redirect-uri#access_token=ACCESS-TOKEN
.
您正在应用程序中使用显式,请尝试更改userURL
tohttps://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token
和tokenURL
to http://your-redirect-uri#access_token=ACCESS-TOKEN
。
回答by Roger Perkins
You most likely cannot use a client secret in an iPhone App (because the client secret cannot be protected), you will need to authenticate through the services mobile app or mobile web site and then handle the redirect url, according to this link I found on the Uber api developer site: http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
您很可能无法在 iPhone 应用程序中使用客户端密码(因为无法保护客户端密码),您需要通过服务移动应用程序或移动网站进行身份验证,然后根据我在上找到的此链接处理重定向 URL Uber api 开发者站点:http: //aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
There is some code on this page to do just that, which I am testing now: https://medium.com/swift-programming/learn-nsurlsession-using-swift-ebd80205f87c
此页面上有一些代码可以做到这一点,我现在正在测试:https: //medium.com/swift-programming/learn-nsurlsession-using-swift-ebd80205f87c
There is also some good information on the bottom part of this page: http://www.idmworks.com/blog/entry/getting-started-with-oauth2client-on-ios
这个页面的底部也有一些很好的信息:http: //www.idmworks.com/blog/entry/getting-started-with-oauth2client-on-ios
回答by Oleg Kohtenko
I recently made simple pod https://github.com/kohtenko/KOSocialOAuth.
我最近做了一个简单的 pod https://github.com/kohtenko/KOSocialOAuth。
You can easily connect Instagram
, VK
or LinkedIn
. Feel free to send Pull Request with any other OAuth endpoint.
您可以轻松连接Instagram
,VK
或LinkedIn
。随意使用任何其他 OAuth 端点发送拉取请求。
回答by DHShah01
Check out this for using the Instagram API: https://github.com/shyambhat/InstagramKit. In the comments I see that you're having trouble with the redirect - look into Xcode's Redirect URI's for help with that: What's a redirect URI? how does it apply to iOS app for OAuth2.0?
查看此以使用 Instagram API:https: //github.com/shyambhat/InstagramKit。在评论中,我看到您在重定向时遇到问题 - 查看 Xcode 的重定向 URI 以寻求帮助:什么是重定向 URI?它如何适用于 OAuth2.0 的 iOS 应用程序?