背景
我正在使用 Lyft API 通过他们的 3 leg Oauth2 流程对用户进行身份验证。我已按照此处的文档Deep Linking
添加到我的应用程序中。
当我的应用程序打开时,它会立即在 Safari 中加载 Lyft 身份验证页面。在他们完成接受我请求的权限的过程后,Safari 尝试重定向到我在Lyft Developer的开发者帐户中设置的 URL
这里的问题是,当用户授予我的应用程序权限时,我需要用户带着 Lyft 给出的响应返回我的应用程序。
我试过的
深层链接
lyftauth://
我可以将该链接键入 Safari,当我在手机上时,如果安装了该应用程序,它会打开我的应用程序。我尝试将该链接添加为 Lyft 开发人员页面上的重定向 URL,但它不接受该 URL 格式。
所以我确定我必须为开发人员帐户页面提供一个 URL 以重定向,我知道它会尝试重定向到该 URL,而且我知道我无法使用正确的 URL 来让我的应用程序打开备份。
React Native Oauth 库
我尝试使用库react-native-oauth。使用这个库时,我发现它没有按预期工作。在 githu.com 上打开了许多问题,其中很多甚至没有回复。我试图为库和编辑代码为我工作,但无论如何我最终都会在一个不存在的对象上调用一个方法。具体来说,一个名为authorize
.
原生 Xcode 应用程序
我使用 Swift 构建了一个 Xcode 项目并安装了Lyft SDK
cocoapods。我能够使用此 SDK 使用 API。由于不断缺少依赖项,我无法将 React Native 合并到现有的 Swift 项目中。
代码示例
使用上面提到的链接文档,我将此代码添加到我的 App Delegate 文件中,
AppDelegate.m
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"Lyft"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
然后我进入 info.plist 并添加了一个链接来打开应用程序,
我正在导入Linking
我的 React Native 组件,
import React, { Component } from 'react';
import {
Linking,
} from 'react-native';
我将 React Native 文档中的代码添加到 React Native 组件中,
const url = 'https://api.lyft.com/oauth/authorize?client_id=PbUe5NjrXqQP&scope=public%20profile%20rides.read%20rides.request%20offline&state=true&response_type=code'
class App extends Component {
constructor(props) {
super(props);
this.state = {
}
this._handleOpenURL = this._handleOpenURL.bind(this);
}
componentDidMount() {
Linking.openURL(url);
Linking.addEventListener('url', this._handleOpenURL);
}
componentWillUnmount() {
Linking.removeEventListener('url', this._handleOpenURL);
}
_handleOpenURL(event) {
console.log(event.url);
console.log('WE ARE TRYING TO CALL THIS FUNCTION AT THIS POINT');
}
render() {
return (
...
);
}
}
export default App;
问题
当您使用的不是专门为您处理Oauth2
时,使用 React Native 处理重定向回您的应用程序的最常见方法是什么?API