use of com.google.auth.oauth2.UserCredentials in project google-auth-library-java by google.
the class UserCredentialsTest method equals_false_clientId.
@Test
public void equals_false_clientId() throws IOException {
final URI tokenServer1 = URI.create("https://foo1.com/bar");
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory();
UserCredentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(httpTransportFactory).setTokenServerUri(tokenServer1).build();
UserCredentials otherCredentials = UserCredentials.newBuilder().setClientId("other client id").setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(httpTransportFactory).setTokenServerUri(tokenServer1).build();
assertFalse(credentials.equals(otherCredentials));
assertFalse(otherCredentials.equals(credentials));
}
use of com.google.auth.oauth2.UserCredentials in project sample-googleassistant by androidthings.
the class Credentials method fromResource.
static UserCredentials fromResource(Context context, int resourceId) throws IOException, JSONException {
InputStream is = context.getResources().openRawResource(resourceId);
byte[] bytes = new byte[is.available()];
is.read(bytes);
JSONObject json = new JSONObject(new String(bytes, "UTF-8"));
return new UserCredentials(json.getString("client_id"), json.getString("client_secret"), json.getString("refresh_token"));
}
use of com.google.auth.oauth2.UserCredentials in project sample-googleassistant by androidthings.
the class AssistantActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "starting assistant demo");
setContentView(R.layout.activity_main);
ListView assistantRequestsListView = findViewById(R.id.assistantRequestsListView);
mAssistantRequestsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mAssistantRequests);
mMainHandler = new Handler(getMainLooper());
assistantRequestsListView.setAdapter(mAssistantRequestsAdapter);
mButtonWidget = findViewById(R.id.assistantQueryButton);
mButtonWidget.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mEmbeddedAssistant.startConversation();
}
});
// Audio routing configuration: use default routing.
AudioDeviceInfo audioInputDevice = null;
AudioDeviceInfo audioOutputDevice = null;
if (USE_VOICEHAT_I2S_DAC) {
audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUS);
if (audioInputDevice == null) {
Log.e(TAG, "failed to find I2S audio input device, using default");
}
audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUS);
if (audioOutputDevice == null) {
Log.e(TAG, "failed to found I2S audio output device, using default");
}
}
try {
if (USE_VOICEHAT_I2S_DAC) {
Log.i(TAG, "initializing DAC trigger");
mDac = VoiceHat.openDac();
mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
mButton = VoiceHat.openButton();
mLed = VoiceHat.openLed();
} else {
PeripheralManager pioManager = PeripheralManager.getInstance();
mButton = new Button(BoardDefaults.getGPIOForButton(), Button.LogicState.PRESSED_WHEN_LOW);
mLed = pioManager.openGpio(BoardDefaults.getGPIOForLED());
}
mButton.setDebounceDelay(BUTTON_DEBOUNCE_DELAY_MS);
mButton.setOnButtonEventListener(this);
mLed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
mLed.setActiveType(Gpio.ACTIVE_HIGH);
} catch (IOException e) {
Log.e(TAG, "error configuring peripherals:", e);
return;
}
// Set volume from preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int initVolume = preferences.getInt(PREF_CURRENT_VOLUME, DEFAULT_VOLUME);
Log.i(TAG, "setting audio track volume to: " + initVolume);
UserCredentials userCredentials = null;
try {
userCredentials = EmbeddedAssistant.generateCredentials(this, R.raw.credentials);
} catch (IOException | JSONException e) {
Log.e(TAG, "error getting user credentials", e);
}
mEmbeddedAssistant = new EmbeddedAssistant.Builder().setCredentials(userCredentials).setDeviceInstanceId(DEVICE_INSTANCE_ID).setDeviceModelId(DEVICE_MODEL_ID).setLanguageCode(LANGUAGE_CODE).setAudioInputDevice(audioInputDevice).setAudioOutputDevice(audioOutputDevice).setAudioSampleRate(SAMPLE_RATE).setAudioVolume(initVolume).setDeviceModelId(DEVICE_MODEL_ID).setDeviceInstanceId(DEVICE_INSTANCE_ID).setLanguageCode(LANGUAGE_CODE).setRequestCallback(new RequestCallback() {
@Override
public void onRequestStart() {
Log.i(TAG, "starting assistant request, enable microphones");
mButtonWidget.setText(R.string.button_listening);
mButtonWidget.setEnabled(false);
}
@Override
public void onSpeechRecognition(List<SpeechRecognitionResult> results) {
for (final SpeechRecognitionResult result : results) {
Log.i(TAG, "assistant request text: " + result.getTranscript() + " stability: " + Float.toString(result.getStability()));
mAssistantRequestsAdapter.add(result.getTranscript());
}
}
}).setConversationCallback(new ConversationCallback() {
@Override
public void onResponseStarted() {
super.onResponseStarted();
// When bus type is switched, the AudioManager needs to reset the stream volume
if (mDac != null) {
try {
mDac.setSdMode(Max98357A.SD_MODE_LEFT);
} catch (IOException e) {
Log.e(TAG, "error enabling DAC", e);
}
}
}
@Override
public void onResponseFinished() {
super.onResponseFinished();
if (mDac != null) {
try {
mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
} catch (IOException e) {
Log.e(TAG, "error disabling DAC", e);
}
}
if (mLed != null) {
try {
mLed.setValue(false);
} catch (IOException e) {
Log.e(TAG, "cannot turn off LED", e);
}
}
}
@Override
public void onError(Throwable throwable) {
Log.e(TAG, "assist error: " + throwable.getMessage(), throwable);
}
@Override
public void onVolumeChanged(int percentage) {
Log.i(TAG, "assistant volume changed: " + percentage);
// Update our shared preferences
Editor editor = PreferenceManager.getDefaultSharedPreferences(AssistantActivity.this).edit();
editor.putInt(PREF_CURRENT_VOLUME, percentage);
editor.apply();
}
@Override
public void onConversationFinished() {
Log.i(TAG, "assistant conversation finished");
mButtonWidget.setText(R.string.button_new_request);
mButtonWidget.setEnabled(true);
}
@Override
public void onAssistantResponse(final String response) {
if (!response.isEmpty()) {
mMainHandler.post(new Runnable() {
@Override
public void run() {
mAssistantRequestsAdapter.add("Google Assistant: " + response);
}
});
}
}
public void onDeviceAction(String intentName, JSONObject parameters) {
if (parameters != null) {
Log.d(TAG, "Get device action " + intentName + " with parameters: " + parameters.toString());
} else {
Log.d(TAG, "Get device action " + intentName + " with no paramete" + "rs");
}
if (intentName.equals("action.devices.commands.OnOff")) {
try {
boolean turnOn = parameters.getBoolean("on");
mLed.setValue(turnOn);
} catch (JSONException e) {
Log.e(TAG, "Cannot get value of command", e);
} catch (IOException e) {
Log.e(TAG, "Cannot set value of LED", e);
}
}
}
}).build();
mEmbeddedAssistant.connect();
}
use of com.google.auth.oauth2.UserCredentials in project google-auth-library-java by google.
the class OAuth2CredentialsTest method addChangeListener_notifiesOnRefresh.
@Test
public void addChangeListener_notifiesOnRefresh() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
OAuth2Credentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
// Use a fixed clock so tokens don't expire
userCredentials.clock = new TestClock();
TestChangeListener listener = new TestChangeListener();
userCredentials.addChangeListener(listener);
Map<String, List<String>> metadata;
assertEquals(0, listener.callCount);
// Get a first token
metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, accessToken1);
assertEquals(accessToken1, listener.accessToken.getTokenValue());
assertEquals(1, listener.callCount);
// Change server to a different token and refresh
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
// Refresh to force getting next token
userCredentials.refresh();
metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, accessToken2);
assertEquals(accessToken2, listener.accessToken.getTokenValue());
assertEquals(2, listener.callCount);
}
use of com.google.auth.oauth2.UserCredentials in project google-auth-library-java by google.
the class UserCredentialsTest method equals_true.
@Test
public void equals_true() throws IOException {
final URI tokenServer = URI.create("https://foo.com/bar");
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
UserCredentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
UserCredentials otherCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
assertTrue(credentials.equals(otherCredentials));
assertTrue(otherCredentials.equals(credentials));
}
Aggregations