I'm new to react native, so bear with me here. I have a react native component calling Plaid Link, which returns a public_token. Once I get that, I call Plaid to exchange the public_token for the access_token, which I need to save as a variable.
Currently, I have this call after Plaid Link returns the public_token. I can't tell exactly where the issue is, but I don't think I'm extracting the access_token from the response properly.
//Call Plaid Public Token Exchange: PublicToken -> AccessToken
fetch("https://sandbox.plaid.com/item/public_token/exchange", {
method: "POST",
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"client_id": "[ClientID]",
"secret": "[SandboxSecretID]",
"public_token": success.publicToken
}),
})
.then(response => response.json())
.then(response => {
console.log(response.content);
setVariable({ key: 'AccessToken', value: access_token })
})
.catch(err => {
console.log(err);
});
This is the response the call should return (again, trying to save access_token)
{ "access_token": "access-sandbox-xxxxx", "item_id": "YYYYYYYY", "request_id": "ZZZZZZZZZ" }
Try to do this:
First declare a useState before everything
const [ApiData, setApiData] = useState(
{ access_token : null, item_id : null, request_id : null }
)
and then after in your code
.then(response => response.json())
.then(response => {
console.log(response.content);
setApiData({
access_token : response.content.access_token
item_id : response.content.item_id
request_id : reponse.content.request_id
})
})
Now you have the access_token stocked in {ApiData.access_token}
Hope this helps and sorry if it doesn't or if I didn't understand your question.