I want to make a image search web app with unsplash api, but I can't seem to get it to work, I am following instructions on here https://github.com/unsplash/unsplash-js#dependencies also there is additional information in here https://unsplash.com/documentation#authorization-workflow. I am new to authorization.
import React, { Component } from 'react';
import Unsplash, { toJson } from 'unsplash-js';
import './App.css';
const unsplash = new Unsplash({
applicationId: '1424074b20f17701ec8c0601fd15ca686c70e2cb0e645f8137533d8063e664bc',
secret: 'd47bcd8287983e24da69c37348b21bee55b4c808390413f52fa6aa545b11debc',
callbackUrl: 'urn:ietf:wg:oauth:2.0:oob',
headers: {
"Accept-Version": "v1"
}
});
class App extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
componentDidMount() {
const authenticationUrl = unsplash.auth.getAuthenticationUrl([
"public",
"read_user",
"write_user",
"read_photos",
"write_photos"
]);
location.assign(authenticationUrl);
unsplash.auth.userAuthentication(query.code)
.then(toJson)
.then(json => {
console.log(json.access_token)
unsplash.auth.setBearerToken(json.access_token);
})
.catch(err => console.log('Auth err', err));
}
handleClick() {
let query = document.getElementById('input').value;
unsplash.search.photos(query, 1) //function provided by api
.then(toJson)
.then(json => {
console.log(json);
});
}
render() {
return (
<div className="App">
<form id='form'>
<input type='text' id='input' ></input>
<input type='submit' id='submit' onClick={this.handleClick} ></input>
</form>
<div id='field' >
</div>
</div>
);
}
}
export default App;
The error:
https://unsplash.com/oauth/authorize?client_id=1424074b20f17701ec8c0601fd15ca686c70e2cb0e645f8137533d8063e664bc&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=public+read_user+write_user+read_photos+write_photos App.js:29
ReferenceError: query is not defined App.js:31
The above error occurred in the <App> component:
in App (at index.js:7)
Consider adding an error boundary to your tree to customize error handling behavior.
index.js:2178
ReferenceError: query is not defined App.js:31
./src/App.js
Line 31: 'query' is not defined no-undef
Search for the keywords to learn more about each error.