Skip to content

Commit

Permalink
[nest] Fix for missing refresh token after reauthorization (openhab#1…
Browse files Browse the repository at this point in the history
…2711)

Signed-off-by: Mark Hilbush <mark@hilbush.com>
  • Loading branch information
mhilbush authored and psmedley committed Feb 23, 2023
1 parent db4f1a4 commit 4fbf8b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bundles/org.openhab.binding.nest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ Finally, an SDM Account Thing can be created to access the SDM project using the
1. Create an authorization code for the binding:
1. Replace the **Project ID** and **Client ID** in the URL below with your SDM Project ID and SDM OAuth 2.0 Client ID and open the URL in a new browser tab:

`https://nestservices.google.com/partnerconnections/<ProjectID>/auth?scope=https://www.googleapis.com/auth/sdm.service&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=<ClientID>`
`https://nestservices.google.com/partnerconnections/<ProjectID>/auth?scope=https://www.googleapis.com/auth/sdm.service&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=<ClientID>`

For the example values used so far this is:

`https://nestservices.google.com/partnerconnections/585de72e-968c-435c-b16a-31d1d3f76833/auth?scope=https://www.googleapis.com/auth/sdm.service&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=1046297811237-3f5sj4ccfubit0fum027ral82jgffsd1.apps.googleusercontent.com`
`https://nestservices.google.com/partnerconnections/585de72e-968c-435c-b16a-31d1d3f76833/auth?scope=https://www.googleapis.com/auth/sdm.service&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=1046297811237-3f5sj4ccfubit0fum027ral82jgffsd1.apps.googleusercontent.com`
1. Enable all the permissions you want to use with the binding and click "Next" to continue
1. Login using your Google account when prompted
1. On the "Google hasn't verified this app" page, click on "Advanced"
Expand Down Expand Up @@ -153,11 +153,11 @@ Finally, the existing SDM Account Thing can be updated so it can subscribe to SD
1. Create an authorization code for the binding:
1. Replace the **Client ID** in the URL below with your Pub/Sub OAuth 2.0 Client ID and open the URL in a new browser tab:

`https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/pubsub&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=<ClientID>`
`https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/pubsub&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=<ClientID>`

For the example client this is:

`https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/pubsub&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=1046297811237-lg27h26kln6r1nbg54jpg6nfjg6h4b3n.apps.googleusercontent.com`
`https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/pubsub&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=1046297811237-lg27h26kln6r1nbg54jpg6nfjg6h4b3n.apps.googleusercontent.com`
1. Login using your Google account when prompted
1. On the "Google hasn't verified this app" page, click on "Advanced"
1. Then click on "Go to ... (advanced)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void run() {
}

try {
checkAccessTokenValidity();
String messages = pullSubscriptionMessages(subscriptionId);

PubSubPullResponse pullResponse = GSON.fromJson(messages, PubSubPullResponse.class);
Expand All @@ -104,7 +105,8 @@ public void run() {
scheduler.schedule(this, RETRY_TIMEOUT.toNanos(), TimeUnit.NANOSECONDS);
}
} catch (InvalidPubSubAccessTokenException e) {
logger.warn("Cannot pull messages for '{}' subscription (access token invalid)", subscriptionId, e);
logger.warn("Cannot pull messages for '{}' subscription (access or refresh token invalid)",
subscriptionId, e);
forEachListener(listener -> listener.onError(e));
} catch (Exception e) {
logger.warn("Unexpected exception while pulling message for '{}' subscription", subscriptionId, e);
Expand Down Expand Up @@ -225,6 +227,10 @@ private String getAuthorizationHeader() throws InvalidPubSubAccessTokenException
throw new InvalidPubSubAccessTokenException(
"No Pub/Sub access token. Client may not have been authorized.");
}
if (response.getRefreshToken() == null || response.getRefreshToken().isEmpty()) {
throw new InvalidPubSubAccessTokenException(
"No Pub/Sub refresh token. Delete and readd credentials, then reauthorize.");
}
return BEARER + response.getAccessToken();
} catch (OAuthException | OAuthResponseException e) {
throw new InvalidPubSubAccessTokenException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ private String getAuthorizationHeader() throws InvalidSDMAccessTokenException, I
if (response == null || response.getAccessToken() == null || response.getAccessToken().isEmpty()) {
throw new InvalidSDMAccessTokenException("No SDM access token. Client may not have been authorized.");
}
if (response.getRefreshToken() == null || response.getRefreshToken().isEmpty()) {
throw new InvalidSDMAccessTokenException(
"No SDM refresh token. Delete and readd credentials, then reauthorize.");
}
return BEARER + response.getAccessToken();
} catch (OAuthException | OAuthResponseException e) {
throw new InvalidSDMAccessTokenException(
Expand Down

0 comments on commit 4fbf8b2

Please sign in to comment.