OAuth2 进行身份验证 API - 无法重定向回我的应用程序

OAuth2 to Authenticate API - Cannot Redirect Back to my App

我正在尝试使用 Trakt API 获取电视节目列表和其他数据。但是,我坚持使用 Trakt 验证我的应用程序。我有我的 API 密钥、秘密和重定向 URI,但正在为如何授权我的应用程序而苦苦挣扎。我尝试了以下方法:

方法一,使用Trakt的示例代码:

-(void)authorisation{

    NSString *redirectURI = @"http://myappredirect://";
    NSString *clientID = @"MY_CLIENT_ID";
    NSString *clientSecret = @"MY_CLIENT_SECRET";
    NSString *username = @"USERNAME";
    NSString *authURL = [NSString stringWithFormat:@"https://trakt.tv/oauth/authorize?response_type=code&client_id=%@&redirect_uri=%@&state=state&username=%@", clientID, redirectURI, username];

    NSURL *URL = [NSURL URLWithString:authURL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"GET"];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [[UIApplication sharedApplication] openURL:URL];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {

                                      if (error) {
                                          // Handle error...
                                          return;
                                      }

                                      if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                          NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
                                          NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
                                      }

                                      NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                      NSLog(@"Response Body:\n%@\n", body);
                                  }];
    [task resume];
}

这会在我的 iPhone 上打开 Safari,成功加载网页,Trakt 要求为我的应用程序授权我的帐户。我点击 'Authorize',然后 Safari 加载 URL 'myappredirect//?code=a_really_long_string_of_characters",但出现错误

Safari cannot open the page because the server cannot be found.

当我在 Safari 中输入 myappredirect:// 时,我的应用程序会打开,所以我想知道 Safari 加载的 URL 是否不正确,因为它在双 // 之前缺少分号?

所以我尝试将 UIWebView 添加到我的应用程序并在其中加载 URL。它会加载 URL,但这次在我点击 'Authorize' 后,它不会更改网页。 UIWebView 委托 webViewDidStartLoad 确实告诉我它在我点击 'Authorize' 后加载了一个页面,但屏幕上没有任何变化。

方法 2, 使用 OAuth2Client:

-(void)setupWebview{

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    webView.backgroundColor = [UIColor greenColor];
    webView.delegate = self;
    [self.view addSubview:webView];

}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad");
}

-(void)secondMethod{
    NSString *redirectURI = @"http://myappredirect://";
    NSString *clientID = @"MY_CLIENT_ID";
    NSString *clientSecret = @"MY_CLIENT_SECRET";
    NSString *username = @"USERNAME";
    NSString *authURL = [NSString stringWithFormat:@"https://api-v2launch.trakt.tv/oauth/authorize?response_type=code&client_id=%@&redirect_uri=%@&state=state&username=%@", clientID, redirectURI, username];
    NSString *tokenURL = @"https://api-v2launch.trakt.tv";

    [[NXOAuth2AccountStore sharedStore] setClientID:clientID
                                             secret:clientSecret
                                   authorizationURL:[NSURL URLWithString:authURL]
                                           tokenURL:[NSURL URLWithString:tokenURL]
                                        redirectURL:[NSURL URLWithString:redirectURI]
                                     forAccountType:@"Trakt"];

    [[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"Trakt"
                                   withPreparedAuthorizationURLHandler:^(NSURL *preparedURL){
                                       // Open a web view or similar
                                       [webView loadRequest:[NSURLRequest requestWithURL:preparedURL]];
                                   }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];

    [[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                      object:[NXOAuth2AccountStore sharedStore]
                                                       queue:nil
                                                  usingBlock:^(NSNotification *aNotification){
                                                      // Update your UI
                                                      NSLog(@"Success");
                                                  }];

    [[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                      object:[NXOAuth2AccountStore sharedStore]
                                                       queue:nil
                                                  usingBlock:^(NSNotification *aNotification){
                                                      NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                      // Do something with the error
                                                      NSLog(@"Error");
                                                  }];
}

在这里,我不确定令牌 URL 是什么。同样,我的 UIWebView 完美地加载了 URL,但是在我按下 'Authorize' 之后,它并没有改变它的网页。委托方法 webViewDidStartLoad 确实告诉我它加载了另一个页面,但屏幕上没有任何变化。此外,NXOAuth2 条通知均未发送。

我是 OAuth2 的新手,非常感谢任何人可能提供的任何帮助。如果这是一个愚蠢的问题,我深表歉意,我真的很纠结该怎么做,并且对为什么在我授权 Trakt 后 Safari 无法打开我的应用程序感到困惑。

谢谢。

NSString *redirectURI = @"myappredirect://";

而不是

NSString *redirectURI = @"http://myappredirect://";

当 url 以重定向 url 开始并从 url 获取参数授权代码时,使用 UIWebView 中断了 UIWebView 委托 shouldStartLoadWithRequest 中的加载,与 "get token url" 连接并调用 [[NXOAuth2AccountStore sharedStore] handleRedirectURL:getTokenURL];

可能不是最好的方法,但对我有用。 :)

英语不是我的第一语言,所以请原谅任何错误。 希望对您有所帮助。

这是我的做法。

第 1 步:编辑您的 Info.plist

您需要定义一个 url 方案来打开您的应用程序而不是浏览器,这对于 oauth 过程结束时的重定向很有用。

例如,我们称它为 yoururlscheme,但您可以随意选择(例如:小写的 "yourappname",甚至您的包标识符 "com.example.yourappname")

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>yoururlscheme</string>
        </array>
    </dict>
</array>

第 2 步:在 trakt.tv

  • 转到您的应用 (https://trakt.tv/oauth/applications/{yourappid})
  • 根据您在第 1 步中选择的内容编辑它并添加到重定向 uri 列表 yoururlscheme://oauth/token

第 3 步:您的 ViewController

import SafariServices

class YourViewController: UIViewController {

  var oauthSession: SFAuthenticationSession? = nil

  func signIn() {
    let clientId = "" // your client id
    let redirectUri = "yoururlscheme://oauth/token"
    let oauthURL = "https://trakt.tv/oauth/authorize?response_type=code&client_id=\(clientId)&redirect_uri=\(redirectUri)"
    oauthSession = SFAuthenticationSession(url: oauthURL, callbackURLScheme: redirectUri) { (url, error) in
        guard let url = url,
            let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
            let queryItems = urlComponents.queryItems,
            let code = queryItems.first(where: {[=11=].name == "code"})?.value else {
            return
        }
        // Do whatever you want with the token now
        print("code: \(code)")
    }
    oauthSession?.start()
  }
}