RE: Create OAuth2 Application from JAVA code in portlet

thumbnail
Jamie Sammons, modified 3 Years ago. New Member Posts: 2 Join Date: 5/2/22 Recent Posts

I am using Eclipse IDE with Liferay version 7.1.3.

I need to be able to create a OAuth 2 Application via code rather than doing it manually from Control Panel/Configuration/OAuth 2 Administration.

This is just a POC, so the Java code will initially be in a background Portlet MVCPortlet Class. 

I have this as an external depenance - com.liferay.oauth2.provider.service-1.0.2-sources.jar

I have this in my module build.gradle - compileOnly group: "com.liferay", name: "com.liferay.oauth2.provider.service"

I have been looking at using OAuth2ApplicationServiceImpl.addOAuth2Application(...) But I am not familiar enough with JAVA to get this working. I assume it needs an import into the portlet.java file.

If anyone has any ideas, I'd be grateful to hear them.

Thanks...in advance.

thumbnail
Jamie Sammons, modified 3 Years ago. New Member Posts: 6 Join Date: 5/1/15 Recent Posts

Hi Robert.

Looks like you added a dependency on the OAuth 2 service implementation JAR and not its API.

The one you want is: com.liferay.oauth2.provider.api

A good example for how to use this API can be found here:

https://github.com/liferay/liferay-portal/blob/a165740c8b1bdbf95c8f89b39cf6738e73b57d0b/modules/apps/oauth2-provider/oauth2-provider-shortcut/src/main/java/com/liferay/oauth2/provider/shortcut/internal/instance/lifecycle/FragmentRendererPortalInstanceLifecycleListener.java#L65-L86 

The scopes assignment is probably the most tricky part, but hopefully with that example you can get it something working.

Let me know how you get on :)

-Stian