如何在React Native中从浏览器检索URL?

时间:2019-04-03 11:34:13

标签: react-native instagram-api

我本机反应很新,我想为我的应用程序创建一个Instagram身份验证,我需要它的访问令牌。如文档中所述,我必须访问此链接https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token,并且当用户授权并验证了该应用程序后,我将被重定向到该链接http://your-redirect-uri#access_token=ACCESS-TOKEN,从中可以获取访问令牌。但是我无法实现此操作,因为我不知道重定向到此http://your-redirect-uri#access_token=ACCESS-TOKEN后如何从react-native中的url检索令牌的过程。

我正在使用WebBrowser(Expo)打开应用程序中的链接。

PS:我在我的react-native-app中使用CRNA cli

2 个答案:

答案 0 :(得分:0)

从示例中可以看出:

    import InstagramLogin from 'react-native-instagram-login'
<View>
    <TouchableOpacity onPress={()=> this.refs.instagramLogin.show()}>
        <Text style={{color: 'white'}}>Login</Text>
    </TouchableOpacity>
    <InstagramLogin
        ref='instagramLogin'
        clientId='xxxxxxxxxx'
        scopes={['public_content', 'follower_list']}
        onLoginSuccess={(token) => this.setState({ token })}
        onLoginFailure={(data) => console.log(data)}
    />
</View>

这表明令牌将在您的应用状态内(调用onLoginSuccess / Failure)。从那里访问令牌。

答案 1 :(得分:0)

您可以通过使用“ https://localhost”作为重定向网址来获得Access令牌,

使用webView

中的链接

例如:

   <WebView
                source={{
                  uri: "https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=https://localhost&response_type=token"
                }}
                onNavigationStateChange={state =>
                  this._onChangeNav(state)
                }
   />

然后添加方法

_onLoadAddNewCardView(state) {

   console.warn("RES=" + state.url);// Your url with ACCESS TOKEN

   let responseString = state.url;
   responseString = responseString.replace("https://localhost/?", "");

   // Here you will get the ACCESS TOKEN
}
相关问题