OAuthClient
public
class
OAuthClient
extends Object
java.lang.Object | |
↳ | android.support.wearable.authentication.OAuthClient |
This class is deprecated.
use androidx.wear.phone.interactions.authentication.RemoteAuthClient
provided
by the Jetpack Wear Phone Interactions
library instead.
Provides a client for supporting OAuth 2.0 on Wear. The authorization UI will appear on the user's phone.
The following example triggers an authorization session to open on the phone.
private OAuthClient mClient;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
mClient = OAuthClient.create(this);
...
}
@Override
public void onDestroy() {
mClient.destroy();
super.onDestroy();
}
public void startOAuthFlow() {
// Construct the redirect_uri used in your OAuth request.
// OAuthClient.WEAR_REDIRECT_URL ensures the response will be received by Wear OS.
// The receiving app's package name is required as the 3rd path component in the redirect_uri.
// This allows Wear to ensure other apps cannot reuse your redirect_uri to receive responses.
String redirectUri = OAuthClient.WEAR_REDIRECT_URL_PREFIX + "com.package.name";
// Construct the appropriate request for the OAuth provider you wish to integrate with.
String requestUrl = "https:// .... &redirect_uri=" + redirectUri;
// Send it. This will open an authentication UI on the phone.
mClient.sendAuthorizationRequest(Uri.parse(requestUrl), new MyOAuthCallback());
}
private final class MyOAuthCallback extends OAuthClient.Callback {
@Override
public void onAuthorizationResponse(Uri requestUrl, Uri resultUrl) {
// Parse the result token out of the result URL and store it, e.g. in SharedPreferences,
// so you can use it later. You'll also want to display a success UI.
...
}
@Override
public void onAuthorizationError(int errorCode) {
// Compare against codes available in OAuthClient.ErrorCode
// You'll also want to display an error UI.
...
}
}
Summary
Nested classes | |
---|---|
class |
OAuthClient.Callback
This class is deprecated.
use |
@interface |
OAuthClient.ErrorCode
This @interface is deprecated.
use |
Constants | |
---|---|
int |
ERROR_PHONE_UNAVAILABLE
Indicates no phone is connected, or the phone that is connected doesn't support 3p OAuth |
int |
ERROR_UNSUPPORTED
Indicates 3p OAuth isn't supported by Wear OS |
String |
LEGACY_WEAR_REDIRECT_URL_PREFIX
This constant is deprecated.
Prefer |
String |
WEAR_REDIRECT_URL_PREFIX
Browsers will redirect URLs prefixed with this constant to the Wear OS phone app using App Links. |
Public methods | |
---|---|
static
OAuthClient
|
create(Context context)
Returns a client that can be used to make OAuth authorization requests. |
void
|
destroy()
Frees any resources used by the client, dropping any outstanding requests. |
void
|
sendAuthorizationRequest(Uri requestUrl, OAuthClient.Callback clientCallback)
Send an OAuth request. |
Protected methods | |
---|---|
void
|
finalize()
|
Inherited methods | |
---|---|
Constants
ERROR_PHONE_UNAVAILABLE
public static final int ERROR_PHONE_UNAVAILABLE
Indicates no phone is connected, or the phone that is connected doesn't support 3p OAuth
Constant Value: 1 (0x00000001)
ERROR_UNSUPPORTED
public static final int ERROR_UNSUPPORTED
Indicates 3p OAuth isn't supported by Wear OS
Constant Value: 0 (0x00000000)
LEGACY_WEAR_REDIRECT_URL_PREFIX
public static final String LEGACY_WEAR_REDIRECT_URL_PREFIX
This constant is deprecated.
Prefer WEAR_REDIRECT_URL_PREFIX
which is hosted on the Google API domain.
This URL will be removed in the future.
Browsers will redirect URLs prefixed with this constant to the Wear OS phone app using App Links. To deliver an OAuth response to your Wear app, set the redirect_uri parameter on the OAuth request to this constant, with your app's package name appended.
For example, if your app's package name is com.package.name, you should set the redirect_uri to LEGACY_WEAR_REDIRECT_URL_PREFIX + "com.package.name".
Constant Value: "https://www.android.com/wear/3p_auth/"
WEAR_REDIRECT_URL_PREFIX
public static final String WEAR_REDIRECT_URL_PREFIX
Browsers will redirect URLs prefixed with this constant to the Wear OS phone app using App Links. To deliver an OAuth response to your Wear app, set the redirect_uri parameter on the OAuth request to this constant, with your app's package name appended.
For example, if your app's package name is com.package.name, you should set the redirect_uri to WEAR_REDIRECT_URL_PREFIX + "com.package.name".
Constant Value: "https://wear.googleapis.com/3p_auth/"
Public methods
create
public static OAuthClient create (Context context)
Returns a client that can be used to make OAuth authorization requests.
Parameters | |
---|---|
context |
Context |
Returns | |
---|---|
OAuthClient |
destroy
public void destroy ()
Frees any resources used by the client, dropping any outstanding requests. The client cannot be used to make requests thereafter.
sendAuthorizationRequest
public void sendAuthorizationRequest (Uri requestUrl, OAuthClient.Callback clientCallback)
Send an OAuth request. This will cause an authorization UI to be presented on the user's phone. This request is asynchronous; the callback provided will be be notified when the request completes.
Parameters | |
---|---|
requestUrl |
Uri : URL that will be opened in browser on phone. The OAuth response should
redirect to the Wear OS companion. See WEAR_REDIRECT_URL_PREFIX
|
clientCallback |
OAuthClient.Callback |
Protected methods
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.