It's sad to see no concrete examples are available when it comes to using OAuth2 package of Flutter.
I'm a beginner, the example mentioned here is tough for me to crack(I learn mostly through videos), but my job requires me to implement OAuth by today itself. So I'm trying my shot here.
This is what I've:
Login API
curl --location --request POST 'https://domain/api/rest/oauth/token' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=<username_here>' \
--data-urlencode 'password=<md5_of_password_here'
I've been given a sample username and password as well..
This is what I've implemented so far..
void checkAuthentication() async {
String username = 'username';
String password = 'password';
String basicAuth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
print(basicAuth);
var body = jsonEncode(
<String, dynamic>{
'grant_type': 'password',
'username': 'username',
'password': 'password',
},
);
Response r = await http.post(
Uri.https('domain', 'api/rest/oauth/token'),
headers: <String, String>{
'authorization': basicAuth,
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
body: body,
);
print(r.statusCode);
print(r.headers);
print(r.body);
}
I'm getting 301 as result..
What can I do to get 200. Please guide me.
Related
I'm trying to create an user authentication system using Auth0 in my Flutter app. The Auth0 REST documentation gave an example of cURL but I didn't find any flutter package which does the job of cURL. So, I used http.
Here's the code:
Future<String> getToken(String userId) async {
final response = await http.post(
Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'), // I used my real subdomain
body: jsonEncode({
'grant_type=client_credentials',
'client_id=my_project_client_id', // I used my real client id
'client_secret=my_project_client_secret', // I used my real client secret
'audience=https://my-auth0-subdomain.auth0.com/api/v2/' // I used my real subdomain
}),
headers: {
'content-type: application/x-www-form-urlencoded'
},
);
final token = jsonDecode(response.body)["access_token"];
return token;
}
This gives me an error that The argument type 'Set<String>' can't be assigned to the parameter type 'Map<String, String>'. on line 10 (headers: {...}). I can resolve this error by using headers: {'content-type': 'application/x-www-form-urlencoded'},.
But this then gives the error from Auth0 {"error":"access_denied","error_description":"Unauthorized"}. The API is set up properly, because on running
curl --request POST \
--url 'https://my-auth0-subdomain.auth0.com/oauth/token' \
--header "content-type: application/x-www-form-urlencoded" \
--data grant_type=client_credentials \
--data 'client_id=my_project_client_id' \
--data client_secret=my_project_client_secret \
--data 'audience=https://my-auth0-subdomain.auth0.com/api/v2/'
it returns a "access_token", "scope", "expires_in" and "token_type".
Please help. It's very important.
Thanks in advance :)
Try to send data as url encoded using :
Map<String, String> myBody= {
'grant_type' : 'client_credentials',
'client_id' : 'my_project_client_id', // I used my real client id
'client_secret' : 'my_project_client_secret', // I used my real client secret
'audience ' : 'https://my-auth0-subdomain.auth0.com/api/v2/' // I used my real subdomain
};
Future<String> getToken(String userId) async {
final response = await http.post(
Uri.parse('https://my-auth0-subdomain.auth0.com/oauth/token'), // I used my real subdomain
body: myBody,
headers: {
'content-type: application/x-www-form-urlencoded'
},
);
final token = jsonDecode(response.body)["access_token"];
return token;
}
I am using dart:http to make post request but it seems I cannot specify HTTP1.1 version which is required by target api. Ideally I need to re-create following request in dart:
curl -X POST --http1.1 "https://api.dummyapi.com/v1/route" -H "accept: text/plain" -H "Authorization: 000000-000000-000000-000000" -H "Content-Type: application/json-patch+json" -d "{}"
My current version is this:
final url = Uri.parse('https://api.dummyapi.com/v1/route');
final response = await http.post(url, headers: {
'Authorization': '000000-000000-000000-000000',
'accept': 'text/plain',
'Content-Type': 'application/json'
});```
Checked the following code example
Future createUserExample(Map<String,dynamic> data) async {
final http.Response response = await http.post(
'https://api.dummyapi.com/v1/route',
headers: <String, String>{
'Authorization': '000000-000000-000000-000000',
'accept': 'text/plain',
'Content-Type': 'application/json'
},
body: data,
);
return response;
I want start app's build using Openshift Rest API https://docs.openshift.com/container-platform/3.7/rest_api/index.html.
What I need:
change source ref (branch) for build
start new build from new branch
without using triggers build or oc tool.
I cann't find how to do that in https://docs.openshift.com/container-platform/3.7/rest_api/apis-build.openshift.io/v1.Build.html
It can be done using OC Tool Analogue:
oc start-build name -n namespase
But I want do this using REST API
Thank you very much!
curl -H "Authorization: Bearer xxx" -H 'Accept: application/json' -XPOST "$openshiftUrl/apis/build.openshift.io/v1/namespaces/YOURNAMESPACE/buildconfigs/CONFIGNAME/instantiatebinary?name=XXXX&namespace=YOURNAMESSPACE" --data-binary #/tmp/eQXEUXr.zip
using axios
return axios({
method: 'post',
url: 'url/apis/build.openshift.io/v1/namespaces/YYY/buildconfigs/XXX/instantiatebinary?name=XXX&namespace=YYY',
data: fs.createReadStream('/tmp/eQXEUXr.zip'),
headers: {
'content-type': `application/octet-stream`,
'Authorization': 'Bearer aaaaaa',
'Accept': 'application/json'
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
httpsAgent: new Agent({
rejectUnauthorized: false
}),
timeout: 300000,
}
I'm trying to make a HTTP request to modify password from users that are stored into WSO2. I'm using the following request:
{
method: 'PUT',
url: domain + '/wso2/scim/Users/' + userId,
rejectUnauthorized: false,
headers: {
Authorization: 'Bearer ' + scimToken,
'Content-Type': 'application/json'
},
json: true,
body: {
userName : 'foo',
password : 'newPassw0rd'
}
}
But response returns a Java exception (I don't attach it here, because is too long and I think that doesn't have sense. Is related with Apache CXF).
I'm so new with SCIM and WSO2, so I think that I'm making a mistake in the request. Does anyone knows what's wrong?
Thanks!
create user with this request:
curl -v -k --user admin:admin --data '{"schemas":[],"name":{"familyName":"gunasinghe","givenName":"hasinitg"},"userName":"hasinitg","password":"hasinitg","emails":[{"primary":true,"value":"hasini_home.com","type":"home"},{"value":"hasini_work.com","type":"work"}]}' --header "Content-Type:application/json" https://localhost:9443/wso2/scim/Users
update password
curl -v -k --user admin:admin -X PUT -d '{"schemas":[],"name":{"familyName":"gunasinghe","givenName":"hasinitg"},"userName":"hasinitg", "password":"pwd123","emails":[{"value":"hasini#wso2.com","type":"work"},{"value":"hasi7786#gmail.com","type":"home"}]}' --header "Content-Type:application/json" https://localhost:9443/wso2/scim/Users/0032fd29-55a9-4fb9-be82-b1c97c073f02
I'm trying to use the rest api of paypal. How I can translate the Curl request to get the token using UrlfetchApp of Apps Script?
Request Sample
curl https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
Thank you in advance
function tryPayPal() {
var tokenEndpoint = "https://api.sandbox.paypal.com/v1/oauth2/token";
var head = {
'Authorization':"Basic "+ Utilities.base64Encode("EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp"),
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
var postPayload = {
"grant_type" : "client_credentials"
}
var params = {
headers: head,
contentType: 'application/x-www-form-urlencoded',
method : "post",
payload : postPayload
}
var request = UrlFetchApp.getRequest(tokenEndpoint, params);
var response = UrlFetchApp.fetch(tokenEndpoint, params);
var result = response.getContentText();
Logger.log(result);
var resultObject = JSON.parse(result);
}