#oauth with java
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
most people just use spring for any backend/api topics. it does oauth out of the box
or a link
appreciaet it
this does regular OAuth right? the API only uses regular OAuth
actually, the link shows how to secure ur own website with oauth
not how to oauth login elsewhere
i need this
do you have a library to help with that
To generate the headers?
The spring one? They said
Here are some for Java
Wait oh
Is the spring one for headers
@lost pike said that it was for server side accept oauth
Why don't you take a look at the documentation?
@sacred radish you don't want spring for your thing
you aren't making a website
you are making an app
thats why i was hesitant to use it
iirc
Consumer keys for two-legged OAuth can be obtained by clicking on the Integration menu item on the left after logging in as an administrator. NOTE: only users with roles that have the Access Schoology API permission can obtain an API consumer key.
go to the two legged part
it seems you just need to make this auth header
Authorization: OAuth realm="Schoology API",
oauth_consumer_key="dpf43f3p2l4k3l03",
oauth_token="",
oauth_nonce="kllo9940pd9333jh",
oauth_timestamp="1200376800",
oauth_signature_method="PLAINTEXT",
oauth_version="1.0",
oauth_signature="kd94hf93k423kf44%26"
which you can definitely do with just the HttpClient built into java
you don't need a special library
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.UUID;
public final class SchoolologyClient {
private final HttpClient client;
private final String oauthConsumerKey;
private final String oauthConsumerSecret;
public SchoolologyClient(
String oauthConsumerKey,
String oauthConsumerSecret
) {
this.client = HttpClient.newHttpClient();
this.oauthConsumerKey = oauthConsumerKey;
this.oauthConsumerSecret = oauthConsumerSecret;
}
private HttpRequest.Builder baseRequest() {
var authorization = "Authorization";
return HttpRequest.newBuilder()
.header(authorization, "OAuth realm=\"Schoology API\"")
.header(authorization, "oauth_consumer_key=\"" +
URLEncoder.encode(this.oauthConsumerKey, StandardCharsets.UTF_8) +
"\""
)
.header(authorization, "oauth_token=\"\"")
.header(authorization, "oauth_nonce=\"" + UUID.randomUUID() + "\"")
.header(authorization, "oauth_timestamp=\"" + Instant.now().getEpochSecond() + "\"")
.header(authorization, "oauth_signature_method=\"PLAINTEXT\"")
.header(authorization, "oauth_version=\"1.0\"")
.header(authorization, "oauth_signature=\"" +
URLEncoder.encode(this.oauthConsumerSecret, StandardCharsets.UTF_8) +
"\""
);
}
}
here is a start
i thought one would make it a bit easier
🤷♂️
for the secret though, do i need to do something special with the Oauth_signature
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍
helo
it should look something like this, all these headers need to be inside the auth header
wait you are doing that XD mb
im making a request to https://api.schoology.com/v1/users/5
its returning <html> <head><title>400 Bad Request</title></head> <body> <center><h1>400 Bad Request</h1></center> </body> </html>
do you know why i could be getting a 400 when making this request?
nope
can you inspect the request
also i think you should be making the request to some endpoint with those headers to get a token
thats how it works with other oauth things i've used before
make the request with those headers to /oauth/request_token
ok
i am using 2 legged so i dont think i need to do this
but ill give it a shot
just did that, still getting badrequest
heres the code
todoResponse = client.send(
HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://api.schoology.com/v1/oauth/request_token"))
.header(authorization, "OAuth realm=\"Schoology API\"")
.header(authorization, "oauth_consumer_key=\"" +
/* URLEncoder.encode(getKey(), StandardCharsets.UTF_8) +
"\""*/
getKey() + "\""
)
.header(authorization, "oauth_token=\"\"")
.header(authorization, "oauth_nonce=\"" + UUID.randomUUID() + "\"")
.header(authorization, "oauth_timestamp=\"" + Instant.now().getEpochSecond() + "\"")
.header(authorization, "oauth_signature_method=\"PLAINTEXT\"")
.header(authorization, "oauth_version=\"1.0\"")
.header(authorization, "oauth_signature=\"" +
URLEncoder.encode(getSecret(), StandardCharsets.UTF_8) +
"\"")
.build(),
HttpResponse.BodyHandlers.ofString()
Detected code, here are some useful tools:
todoResponse = client.send(HttpRequest.newBuilder().GET().uri(URI.create("https://api.schoology.com/v1/oauth/request_token")).header(authorization, "OAuth realm=\"Schoology API\"").header(authorization, "oauth_consumer_key=\"" + /* URLEncoder.encode(getKey(), StandardCharsets.UTF_8) +
"\""*/
getKey() + "\"").header(authorization, "oauth_token=\"\"").header(authorization, "oauth_nonce=\"" + UUID.randomUUID() + "\"").header(authorization, "oauth_timestamp=\"" + Instant.now().getEpochSecond() + "\"").header(authorization, "oauth_signature_method=\"PLAINTEXT\"").header(authorization, "oauth_version=\"1.0\"").header(authorization, "oauth_signature=\"" + URLEncoder.encode(getSecret(), StandardCharsets.UTF_8) + "\"").build(), HttpResponse.BodyHandlers.ofString()
that looks uglier...
Anyway I found a module that can maybe make this a bit easier
Called Scribe, I put it in my pom but when I put it in my project requirements it shows red even after running a maven command
ummmmm maybe
my key and secret have my real name on it so im not usre
ummmm
there could be i can do some digging
here it says how to test your app
https://developers.schoology.com/app-platform/testing-your-app
but it doesn't say anything about how to do any fo that
oh you can register and become an app developer
ok i just made an app developer account and I have creds for a test school
let me dm them to you
ok just sent them over @quartz horizon
ok i gave this a shot
how would i pass in params for a POST request? @quartz horizon
1 sec
trying the oauth thing
@sacred radish what is the base url i should be using?
oh i got it to work
.header("Authorization", "OAuth realm=\"Schoology API\", " +
"oauth_consumer_key=\"" + key + "\", " +
"oauth_token=\"\", " +
"oauth_nonce=\"" + UUID.randomUUID() + "\", " +
"oauth_timestamp=\"" + Instant.now().getEpochSecond() + "\", " +
"oauth_signature_method=\"PLAINTEXT\", " +
"oauth_version=\"1.0\", " +
//"oauth_signature=\"" + URLEncoder.encode(getSecret(), StandardCharsets.UTF_8) + "\"")
"oauth_signature=\"" + secret + "%26\"")
you need to define the key and secret
but that works
huh, cool
well to pass in params
.POST(HttpRequest.BodyPublishers.ofString(
"{}"
))
use a body publisher
.POST(HttpRequest.BodyPublishers.ofString(Json.writeString(thing)))
something like this
it will be most likely String, String
it may need to be an Object for the value as they could be integers
how is this map getting to you?
i will create it most likely
are you doing anything to it but just passing it in here?
JsonObject o = Json.objectBuilder()
.put("thing", "abc")
.put("other", 123)
.build();
Map<String, Json> o2 = o;
JsonObject is just a map of String to Json so if you need the map part and Map<String, Object> would have worked...
oh ok
thanks for this, im just making functions to make requests with the headers and stuff
i will probs also do PUT and DELETE even though idk how to use either of them i just use POST and GET
I'm doing it like this;
public static Json post(URI uri, Map<String, Object> params){
How would i insert all params from the params into a json?
oh wait i forgot about putAll(
oh but object is not json encodeable
wait how can I do this so putAll will work?
thats what im confused about
why not just
public static Json post(URI uri, Json params)
object is not json encodeable
Yeah Object can be literally anything
public static Json post(URI uri, Json params){
I want to have one that takes a map of params, even if it just calls the Json version of the method
well for me i will need it to be a string, integer or maybe a long/double
public static Json post(URI uri, Map<String, Json> params){
Json.of( on params should get you Json again
but if you want the object one
im not sure, but i may need to POST user objects
let me confirm that
no i wont it will just be a json map
public static Json post(URI uri, Map<String, Object> params){
JsonObject.Builder jsonParamsBuilder = Json.objectBuilder();
for (var entry : params.entrySet()) {
Json value;
if (entry.getValue() instanceof String s) {
value = Json.of(s);
}
else if (entry.getValue() instanceof Integer i) {
value = Json.of(i);
}
else if (entry.getValue() instanceof Long l) {
value = Json.of(l);
}
else if (entry.getValue() instanceof Double d) {
value = Json.of(d);
}
else if (entry.getValue() instanceof JsonEncodable encodable) {
value = encodable.toJson();
}
else if (entry.getValue() == null) {
value = Json.ofNull();
}
else {
throw new IllegalArgumentException();
}
jsonParamsBuilder.put(entry.getKey(), value);
}
Json jsonParams = jsonParamsBuilder.build();
}
ok thanks
if you really need that contract
ok now in this func ```java
public static Json post(URI uri, Json params){
how would i do this part?
```java
.POST(HttpRequest.BodyPublishers.ofString(params))
would i have to call toString??
Json.writeString
but actually toString on a JsonObject would work
(does the same thing)
lmk if that works
Json params = Json.objectBuilder().put("school_uid", "test0").put("user_first", "test1").put("user_last", "test2").put("primary_email", "test3").put("role_id", "test4").put("additional_buildings", "456").build();
Json json = post("https://api.schoology.com/v1/users",params);
im getting Invalid json from the API
public static Json post(URI uri, Json params){
HttpResponse<String> todoResponse;
try {
todoResponse = client.send(
HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(params.toString()))
.uri(uri)
.header("Authorization", "OAuth realm=\"Schoology API\", " +
"oauth_consumer_key=\"" + key + "\", " +
"oauth_token=\"\", " +
"oauth_nonce=\"" + UUID.randomUUID() + "\", " +
"oauth_timestamp=\"" + Instant.now().getEpochSecond() + "\", " +
"oauth_signature_method=\"PLAINTEXT\", " +
"oauth_version=\"1.0\", " +
//"oauth_signature=\"" + URLEncoder.encode(getSecret(), StandardCharsets.UTF_8) + "\"")
"oauth_signature=\"" + secret + "%26\"")
.header("Accept", "application/json")
//.header("Host", "api.schoology.com")
.header("Content-Type", "application/json")
.build(),
HttpResponse.BodyHandlers.ofString()
);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
try{
return Json.readString(todoResponse.body());
} catch (Exception e){
System.out.println("Invalid json");
throw e;
}
}
I have a static method for a string versoin too
public static Json post(String uri, Json params){
return post(URI.create(uri), params);
}
maybe i should add double string
i mean - tpriy primary email as [email protected]?
good idea
we have this now
Json params = Json.objectBuilder().put("\"school_uid\"", "\"test0\"").put("\"user_first\"", "\"test1\"").put("\"user_last\"", "\"test2\"").put("\"primary_email\"", "\"test3\"").put("\"role_id\"", "\"test4\"").put("\"additional_buildings\"", "\"456\"").build();
still nothing...
do you think the quote escapes are the issue?
No
You don't need to do that
Usually when I have trouble with an API request I first make it work in Postman or curl
Wait, yeah don't do "\"abc\"" just do "abc"
If you've been doing that the whole time that is probably the issue
i removed the string escapes, same issue
ill try this in a quick python and see how that works out
ok im getting a 401