Skip to content

Most visited

Recently visited

navigation

OAuthClient

public class OAuthClient
extends Object

java.lang.Object
   ↳ android.support.wearable.authentication.OAuthClient


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 Android Wear.
   // 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 callback is notified when an async OAuth request completes. 

@interface OAuthClient.ErrorCode

Errors returned in onAuthorizationError(int)

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 Android Wear

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.

String WEAR_REDIRECT_URL_PREFIX

Browsers will redirect URLs prefixed with this constant to the Android Wear 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

From class java.lang.Object

Constants

ERROR_PHONE_UNAVAILABLE

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

int ERROR_UNSUPPORTED

Indicates 3p OAuth isn't supported by Android Wear

Constant Value: 0 (0x00000000)

LEGACY_WEAR_REDIRECT_URL_PREFIX

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 Android Wear 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

String WEAR_REDIRECT_URL_PREFIX

Browsers will redirect URLs prefixed with this constant to the Android Wear 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

OAuthClient create (Context context)

Returns a client that can be used to make OAuth authorization requests.

Parameters
context Context

Returns
OAuthClient

destroy

void destroy ()

Frees any resources used by the client, dropping any outstanding requests. The client cannot be used to make requests thereafter.

sendAuthorizationRequest

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 Android Wear companion. See WEAR_REDIRECT_URL_PREFIX

clientCallback OAuthClient.Callback

Protected methods

finalize

void finalize ()

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)