3

背景

我正在使用 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 SDKcocoapods。我能够使用此 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

4

1 回答 1

7

由于这是一个很难克服的问题,而且这个问题并没有受到太多关注,我怀疑未来的其他人会欣赏我如何克服这个问题的一个例子。

问题

在用户使用 Lyft API 3 支路 Oauth 流程接受权限后处理重定向。

解决方案

示例解决方案回购在这里

为了解决这个问题,我使用了 React Native 支持的 Deep Linking。我还必须在 IOS 和 Android 应用程序中设置链接。这些链接需要与 Lyft 开发者页面中的重定向 URL 相同,这样当链接在带有应用程序的移动设备上被触发时,应用程序可以重新打开。这可以按如下方式完成,

  1. 设置深度链接。可以在此处找到有关如何将链接添加到您的应用程序的说明。

  2. 在那个 React Native Link 上没有解释 URL。以下是每个操作系统的深层链接资源。苹果/安卓

  3. 您必须在开发者页面中为您的 Lyft App 添加重定向 URL。此 URL 将被优化到每个操作系统(IOS 和 ANDROID)的本机应用程序设置中。您将在此处的 Lyft 开发者应用页面中设置重定向 URL 。

代码示例

安卓

AndroidManifest.xml

<intent-filter android:label="lyft-app-authorize">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="http"
        android:host="lyft-app"
        android:pathPrefix="/authorize" />
</intent-filter>
<intent-filter android:label="lyft-app-authorize">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="lyft-app"
        android:host="authorize" />
 </intent-filter>

IOS

信息列表

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>lyft-app</string>
    </array>
  </dict>
</array>

AppDelegate.swift

- (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];
}

Lyft 开发者应用页面

应用管理页面

在此处输入图像描述

反应原生

加载 URL / 处理重定向

  componentDidMount() {
        Linking.openURL(url);
        Linking.addEventListener('url', (responseUrl) => {
          console.log(responseUrl);
        });
      }
于 2017-12-03T17:52:48.937 回答