I am trying the write a program that uses the public API for the online investment broker Questrade.
You manually generate a refresh token on the website, and then pass it to a request to get an access token.
Use the token you copied to redeem it for an access token and the server or practice server URL using the following command:
https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=
or
https://practicelogin.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=
I structured the GET request in postman as follows, and it succeeds and receives a valid json body.
https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=MT1men5gBhSXyv15e6j7gC4CrD8msq3P0
In dart I structure the request as follows...
static Future<AccessToken> getAccessToken(String refreshToken) async {
var uri = Uri.https("login.questrade.com", "/oauth2/token?grant_type=refresh_token&refresh_token=$refreshToken");
var response = await http.get(uri);
print(response);
var json = jsonDecode(response.body);
return AccessToken.fromJson(json);
}
But all it give me is an actually html response that looks like it is a login page(too big to post).
So it seems like the endpoint is treating these requests differently, but I can not figure out what is different about these two requests...
CodePudding user response:
You are creating the Uri
wrong since you are using the path as a way to encode data which does not work since it will try encode &
. You can see the problem if you print your uri
object:
https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=MT1men5gBhSXyv15e6j7gC4CrD8msq3P0
Instead, do something like this where you provide the data to the Uri.https
constructor:
void main() {
var refreshToken = 'MT1men5gBhSXyv15e6j7gC4CrD8msq3P0';
var uri = Uri.https(
'login.questrade.com',
'/oauth2/token',
<String, String>{
'grant_type': 'refresh_token',
'refresh_token': refreshToken,
},
);
print(uri);
// https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=MT1men5gBhSXyv15e6j7gC4CrD8msq3P0
}
CodePudding user response:
The solution was not to use Uri.https but rather Uri.parse
var uri = Uri.parse("https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=$refreshToken");