V3 Migration: How to link a google calendar with a refresh token to a scheduler config for availability

Hello, currently I’m linking a google account using a https://oauth2.googleapis.com/token response. I already have the refreshToken from google and pass it to nylas v2 java sdk in order to get a Nylas AccessToken that i later use to link a calendar for that google account to a scheduler configuration that will check availability given it’s primary virtual calendar plus, the calendars linked to the google account/nylas token. How can i do this in v3? Can’t seem to find any straightforward answer to this. I actually asked this through a support ticket and they suggested for me to ask here, thanks.

// Here’s my v2 java code:
com.nylas.models.Response application = nylasClient.applications()
.getDetails();

    HostedAuthentication authentication = application.getData()
            .getHostedAuthentication();

    GoogleProviderSettings googleSettings = ProviderSettings.google()
            .googleClientId(googleClientId)
            .googleClientSecret(googleClientSecret)
            .googleRefreshToken(googleRefreshToken);

    NativeAuthentication.AuthRequestBuilder authRequest = authentication.authRequest()
                                                    .name("Google Calendar")
                                                    .emailAddress(userEmail)
                                                    .providerSettings(googleSettings)
                                                    .scopes(Scope.CALENDAR_READ_ONLY);

    String authorizationCode = authRequest.execute();
    AccessToken token = authentication.fetchToken(authorizationCode);

// Use the accessToken and accountId to actually link the calendar later.
token.getAccessToken();
token.getAccountId();

hi @alexis.dejesus, welcome to the forums!

There’s a lot wrapped up in your question, so let me try to hone in on where your problem is.

What I am getting is:

  • Your v2 app is using Native Authentication as you are supplying your own Google refresh tokens
  • You have provided working code to turn a Google refresh token into a Nylas access token in your v2 app
  • You want to know how to do the same thing in v3 - that is, get a Nylas token that you can then use to make other requests for this account
  • While you provided some high level info about what you are trying to do with that access token, you don’t provide any code for this - so I’m assuming where you’re getting stuck is getting an equivalent token in the first place

Please correct me if any of the above is incorrect.

Native Authentication is nearly the same in v3 as it is in v2. Here are the docs, including code snippets on how to set it up in each SDK: Create grants with Custom authentication | Nylas Docs

Does this documentation help? If not, I can try to directly translate your v2 code snippet for you. But I don’t want to go through the setup for that if I’m not correct about where your problem lies.

1 Like

Thanks @spang Yes, you’re totally right, I wasn’t sure if creating a nylas token to do the same thing I’m currently doing(just link the calendar to the scheduler config for availability) I think I was told that I could set the grantId as part of the availability calendars when creating the config and nylas would take care of this?

So going back to the current issue here, I tried the documentation you shared, and I’m getting a 400 error. I can share the details, maybe I’m missing something? Thanks again :slight_smile:

2024-10-13T11:49:44.432-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Summary                   : => POST https://api.eu.nylas.com/v3/connect/custom reqBodySize=205
2024-10-13T11:49:44.432-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Headers                   : =>
  Authorization: <not logged>
  User-Agent: Nylas Java SDK 2.4.0
  Accept: application/json
  Content-Type: application/json
  Content-Length: 205
  Host: api.eu.nylas.com
  Connection: Keep-Alive
  Accept-Encoding: gzip
2024-10-13T11:49:44.433-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Body                      : =>
{"provider":"google","settings":{"refresh_token":"$VALID_TOKEN"},"state":"<SCOPE>","scope":["calendar.read_only"]}
2024-10-13T11:49:44.788-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Summary                   : <= 400 Bad Request resBodySize=221 durationMs=355
2024-10-13T11:49:44.789-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Headers                   : <=
  Connection: keep-alive
  Content-Length: 221
  x-xss-protection: 1; mode=block
  cache-control: no-store
  via: 1.1 google, 1.1 varnish
  x-content-type-options: nosniff
  x-frame-options: deny
  access-control-allow-origin: *
  strict-transport-security: max-age=63072000; includeSubDomains
  content-type: application/json
  Accept-Ranges: bytes
  Date: Sun, 13 Oct 2024 17:49:44 GMT
  X-Served-By: cache-dfw-kdfw8210051-DFW, cache-dfw-kdfw8210051-DFW
  X-Cache: MISS, MISS
  X-Cache-Hits: 0, 0
  X-Timer: S1728841785.543462,VS0,VE287
  Vary: Origin
  x-fastly-id: 791127253
2024-10-13T11:49:44.789-06:00 DEBUG 89896 --- [hedulingAPIBG-1] com.nylas.http.Body                      : <=
{"request_id":"1729604571-014bb961-8706-4ef2-9825-0b7e2e9121c7","error":{"type":"oauth2.oauth_provider_error","message":"Error from OAuth2 provider","provider_error":{"oauth2":"\"unauthorized_client\" \"Unauthorized\""}}}
2024-10-13T11:49:44.848-06:00 ERROR 89896 --- [hedulingAPIBG-1] a.t.a.s.service.PatientServiceManager    : Error setting up Google calendar from Google

com.nylas.models.NylasApiError: Error from OAuth2 provider
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[na:na]

Can you share the code you’re running to generate that error? Please make sure not to share any of your code’s secrets on the forums.

NylasClient nylasClient = getNylasClient();

        AuthProvider provider = AuthProvider.GOOGLE;

        Map<String, String> settings = new HashMap<String, String>();
        settings.put("refresh_token",
                googleRefreshToken);

        List<String> scopes = List.of("calendar.read_only");

        // Create the request body
        CreateGrantRequest requestBody = new CreateGrantRequest(provider,
                settings,
                "<SCOPE>",
                scopes);

        // Generate the grant
        Response<Grant> auth = nylasClient.auth()
                .customAuthentication(requestBody);

        System.out.println(auth);

Even the direct curl/postman gives me the same error.

curl --location 'https://api.eu.nylas.com/v3/connect/custom' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer $NYLAS_SECRET' \
--header 'Content-Type: application/json' \
--data '{
    "provider": "google",
    "settings": {
        "refresh_token": "$VALID_TOKEN"
    },
    "state": "<SCOPE>",
    "scope": [
        "calendar.read_only"
    ]
}'

Thanks again :slight_smile: