react-native-azure-ad icon indicating copy to clipboard operation
react-native-azure-ad copied to clipboard

console.error: "ERROR:", "ADLoginView._getResourceAccessToken", {}

Open AnthonyNTK opened this issue 7 years ago • 6 comments

ERROR: ADLoginView._getResourceAccessToken Error: Failed to acquire token for resources

React native app code as below

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, { Component } from 'react';

import {
  StyleSheet,
  Text,
  Button,
  View
} from 'react-native';

import {ReactNativeAD, ADLoginView, Logger} from 'react-native-azure-ad';

const CLIENT_ID = '6dcb8e63-5482-45a9-8c30-c701b25c9834';

export default class App extends Component {
  constructor(props) {
    super(props)
    this.AzureADContext = {
      client_id : CLIENT_ID,
      resources : [
        'https://graph.microsoft.com',
        'https://outlook.office.com',
        'https://outlook.office365.com',
        'https://wiadvancetechnology.sharepoint.com',
        'https://graph.windows.net',
      ]
    }

    this.state = {};
  }

  render() {
    new ReactNativeAD({
      client_id: CLIENT_ID,
      resources: [
        'https://graph.microsoft.com',
        'https://outlook.office.com',
        'https://outlook.office365.com',
        'https://wiadvancetechnology.sharepoint.com',
        'https://graph.windows.net',
      ]
    })

    return <ADLoginView
              context={ReactNativeAD.getContext(CLIENT_ID)}
              onSuccess={this.onLoginSuccess.bind(this)}/>
  }

  onLoginSuccess(credentials) {
    console.log(credentials['https://outlook.office365.com'].access_token)
    // use the access token ..
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 20,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

Any idea?

Package.json

{
  "name": "LoginTest",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.2.0",
    "react-native": "0.53.3",
    "react-native-azure-ad": "^0.2.4"
  },
  "devDependencies": {
    "babel-jest": "22.4.1",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.2",
    "react-test-renderer": "16.2.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

AnthonyNTK avatar Feb 23 '18 07:02 AnthonyNTK

Same problem. Looks like that the library is unmaintained

baltuonis avatar Feb 28 '18 14:02 baltuonis

This is most likely not an issue with library, but rather with your azure app registration.

please verify those two potential fixes:

  1. create special URL that needs to be hit by tenant administrator: https://login.microsoftonline.com/<tenant_name_in_onmicrosoft.com-form>/oauth2/authorize?client_id=<appId>&response_type=code&redirect_uri=http://<appUri>&nonce=1234&resource=https://graph.windows.net&prompt=admin_consent

  2. make sure that your native application has got API you try to call in required permissions. This is most likely caused by this app: 'https://wiadvancetechnology.sharepoint.com', in your resources

general-albatross avatar Mar 08 '18 11:03 general-albatross

Same problem. I used the exact same code as https://github.com/wkh237/rn-azure-ad-sample/blob/master/index.common.js

Any idea?

glautrou avatar Nov 29 '18 16:11 glautrou

In constructor the resources option is the issue. Please try to mention graph api url both in constructor and your render method.

Also configure graph api properly on azure portal. You can check Microsoft official document to configure your graph api on azure portal

It worked for me

divyamakkar26 avatar Dec 27 '18 08:12 divyamakkar26

I got this error when my onSuccess method did not have a this binding. Converting it into an arrow function solved the issue.

glocore avatar Jan 09 '19 13:01 glocore

I had the same issue and found the problem in authority url. FYI, I was using react-native. Change the authority from authority: 'https://login.microsoftonline.com/common/v2.0' -> authority: 'https://login.windows.net/common' and rest of the fields are as follows: resourceId: 'https://graph.windows.net/', clientId: '3333387f-2XX3-4XXX-bXXX-0ce3e079XXX', iosRedirectUri: 'x-msauth-org-reactjs-native-example-sample://org.reactjs.native.example.sample', androidRedirectUri: 'msal3333387f-2XX3-4XXX-bXXX-0ce3e079XXX://auth', brokeredAuth: false

raj4all avatar Mar 19 '19 23:03 raj4all