I have an app connected with Azure backend. I created a login and some api calls 2 months ago. They worked fine until a few days ago and then it starts to fail "sometimes".
The login log onFailure says: Error while authenticating user
The callback log onFailure says: Error while processing request
And the cause of both says : stream was reset: PROTOCOL_ERROR
This post is to similar to this but didn't work.
Some code here:
LoginFragment.java
private void login(String email, String password){
loginProgressBar.setVisibility(View.VISIBLE);
try {
JsonObject params = new JsonObject();
params.addProperty("Username", email);
params.addProperty("Password", password);
ListenableFuture<MobileServiceUser> listenable = Client.logIn(getContext(), params);
Futures.addCallback(listenable, new FutureCallback<MobileServiceUser>() {
#Override
public void onSuccess(MobileServiceUser mobileServiceUser) {
loginProgressBar.setVisibility(View.GONE);
SharedPreferences settings = getActivity().getSharedPreferences(Client.MS_USER,0);
SharedPreferences.Editor editor = settings.edit();
Client.clientId = mobileServiceUser.getUserId();
Client.token = mobileServiceUser.getAuthenticationToken();
editor.putString(Client.MS_USER_ID, Client.clientId);
editor.putString(Client.MS_AUTH_TOKEN, Client.token);
editor.apply();
Client.getInstance(getContext()).setCurrentUser(mobileServiceUser);
Intent i = new Intent(getContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onFailure(Throwable t) {
loginProgressBar.setVisibility(View.GONE);
Throwable t2 = t.getCause();
Throwable t3 = t2.getCause();
Log.e("LoginFail", t.getMessage());
Log.e("LoginFail", t2.getMessage());
if(t3 != null){
Log.e("LoginFail", t3.getMessage());
}
Toast.makeText(getContext(), getResources().getString(R.string.bad_login), Toast.LENGTH_LONG).show();
}
}, MoreExecutors.directExecutor());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
Client.java
public class Client {
public static final String MS_USER = "MS_USER";
public static final String MS_USER_ID = "MS_USER_ID";
public static final String MS_AUTH_TOKEN = "MS_AUTH_TOKEN";
public static String clientId;
public static String token;
private static MobileServiceClient instance = null;
public static MobileServiceClient getInstance(Context context) {
if (instance ==null){
try {
instance = new MobileServiceClient(Env.AZURE_URL, context);
instance.setAndroidHttpClientFactory(() -> {
OkHttpClient client = new OkHttpClient();
client.setReadTimeout(20, TimeUnit.SECONDS);
client.setWriteTimeout(20, TimeUnit.SECONDS);
return client;
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else{
instance.setContext(context);
}
return instance;
}
public static ListenableFuture<MobileServiceUser> logIn(Context context, JsonObject parameters) throws MalformedURLException {
String deviceID = "gcm:" + Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
parameters.addProperty("device_id", deviceID);
parameters.addProperty("device_dateTime", Env.DATE_FORMAT.format(new Date()));
parameters.addProperty("device_timeZone", API.getTimezone());
parameters.addProperty("device_language", Env.LANGUAGE);
parameters.addProperty("app", Env.APP_NAME);
return getInstance(context).login("auth", parameters);
}
public static ListenableFuture<JsonElement> callApi(Context context, String apiName, JsonObject parameters, String httpMethod){
if(httpMethod.equals("POST")){
String deviceID = "gcm:" + Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
parameters.addProperty("user_id", Client.clientId);
parameters.addProperty("device_id", deviceID);
parameters.addProperty("device_dateTime", Env.DATE_FORMAT.format(new Date()));
parameters.addProperty("device_timeZone", API.getTimezone());
parameters.addProperty("device_language", Env.LANGUAGE);
parameters.addProperty("app", Env.APP_NAME);
parameters.addProperty("role", "Patient");
return getInstance(context).invokeApi(apiName, parameters, httpMethod, null);
} else {
return getInstance(context).invokeApi(apiName, null, httpMethod, null);
}
}
This is probably related to an issue in Azure App Service that is weirdly enough not reported on the public Azure status page.
The message that affected Azure client received was (quoted from the link above):
Starting at 02:00 UTC on 3 Apr 2018, you have been identified as a
customer using App Services who may have received connection failure
notifications when using Android apps with older HTTP clients or
desktop browsers using cross-site scripting calls. Engineers have
identified an issue with a recent deployment and are investigating
mitigation options. Customers experiencing this issue can
self-mitigate by updating the site config setting "http20Enabled" to
false via resources.azure.com. Instructions on how to update site
config can be found here:
https://azure.microsoft.com/en-us/blog/azure-resource-explorer-a-new-tool-to-discover-the-azure-api/
Go to https://resources.azure.com/
Make sure you are in Read/Write mode by clicking in the option to the
left of your name.
Find the affected site and browse to Config > Web:
https://resources.azure.com/subscriptions//resourceGroups//providers/Microsoft.Web/sites//config/web
Change the property: "http20Enabled": from true to false by clicking
in Edit properties, Update to “false” and then clicking PUT to save
change.
If you have tried these steps and are continuing to experience issues
with your App Service, please create a technical support ticket to
further troubleshoot: aka.ms/azsupt. This message will be closed in 7
days.
Related
My app sends an activation code to email during user's registration process, but for an unknown -to me- reason in some Chinese devices is not working and I'm not being able to get a log of the Exception.
This is where I send the email:
private void getAwsResult_getAwsUser(String strResult)
{
try{
CommunityUserDTO communityUser = CommunityUserService.deserializeUser(strResult);
if(communityUser==null){ //email don't exist in database
registerResult.setValue(new Pair<>(null, Enum.RegisterResult.SENDINGEMAIL));
CreateUserUseCase.setActivationCode(this.user);
sendConfirmationEmail(this.user.getEmail(), this.user.getActivationCode());
}else{
registerResult.setValue(new Pair<>(null, Enum.RegisterResult.EMAILALREADYREGISTERED));
}
}catch(Exception ex)
{
ExceptionHandler.logException(ex);
}
}
private void sendConfirmationEmail(String recipient, int activationCode)
{
String jsonEmail = SendEmailUseCase.
generateJsonConfirmationEmail(recipient, activationCode);
TaskRunner taskRunner = new TaskRunner();
taskRunner.executeAsync(new SendEmailTask(jsonEmail), this::getAwsResult_sendEmail);
}
public class SendEmailTask implements Callable<String>
{
private final String jsonEmail;
public SendEmailTask(String jsonEmail)
{
this.jsonEmail = jsonEmail;
}
#Override
public String call()
{
return TMEmail.send(jsonEmail);
}
}
This is a jsonEmail input parameter for sendEmail method example:
{
"subject":"Your activation code.",
"message":"Your activation code is: xxxxxx",
"send_to":"xxx#gmail.com",
"send_from":"contact#xxx.com",
"smtp_server":"xxx.amazonaws.com",
"smtp_user":"xxx",
"smtp_pass":"xxx",
"smtp_useauth":"true",
"smtp_port":"25",
"center_text":"true"
}
public static String send(String jsonEmail)
{
String result = Enum.Result.OK;
JSONObject oJsonEmail = TMJson.str2JSON(jsonEmail);
try {
String subject = oJsonEmail.getString(Enum.Email.SUBJECT);
String message = oJsonEmail.getString(Enum.Email.MESSAGE);
String sendFrom = oJsonEmail.getString(Enum.Email.SEND_FROM);
String sendTo = oJsonEmail.getString(Enum.Email.SEND_TO);
String smtpServer = oJsonEmail.getString(Enum.Email.SMTP_SERVER);
String smtpPort = oJsonEmail.getString(Enum.Email.SMTP_PORT);
String smtpUseAuth = oJsonEmail.getString(Enum.Email.SMTP_USEAUTH);
boolean centerBodyText = Boolean.parseBoolean(oJsonEmail.getString(Enum.Email.CENTER_TEXT));
final String smtpUser = oJsonEmail.getString(Enum.Email.SMTP_USER);
final String smtpPass = oJsonEmail.getString(Enum.Email.SMTP_PASS);
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", smtpUseAuth);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.from.alias", "My App Name");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser, smtpPass);
}
});
message = TMJson.decodeEmail(message);
message = format(message, centerBodyText);
Message oMessage = new MimeMessage(session);
oMessage.setFrom(new InternetAddress(sendFrom, session.getProperty("mail.from.alias"), "UTF-8"));
oMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo));
oMessage.setSubject(subject);
oMessage.setContent(message, "text/html; charset=iso-8859-2");
Transport.send(oMessage);
}catch (MessagingException | UnsupportedEncodingException | JSONException e) {
result = Enum.Result.KO;
ExceptionHandler.logException(e);
}
return result;
}
I've tested this in my Android mobile and tablet, and in many other devices and it's working just fine, the email is sent and the user receives the activation code, but in China, where we are still testing the beta version the tester is not receiving the email.
I know the app is crashing before sending the email, but cannot figure out why, and if you notice, I hace a try/catch with this ExceptionHandler.logException(e); that sends a report of the crash to my email, but it's also not working, so I don't know what to do.
I contacted AWS support team to check if there is any possibility to see any SES logs, but has no response yet.
Any ideas on how could I get closer to the real exception? Still cannot even see it in the Google Play Developer Console crashes report section.
Ok, luckily I found the issue myself. Nothing to do with Chinese devices, but with Android version.
I was testing my app in Android 10 devices (the only ones I have) and they were testing in an Android 11 device.
This link solved my problem:
Android Studio VerifyError rejecting class text_plain from JavaMail API
And this is the solution: Add this in build.gradle
android {
...
packagingOptions {
exclude 'META-INF/NOTICE.md'
exclude 'META-INF/LICENSE.md'
}
}
dependencies {
implementation 'com.sun.mail:android-mail:1.6.6'
implementation 'com.sun.mail:android-activation:1.6.6'
...
}
Hope I'm helping anyone else having problems sending emails.
I am developing simple Android app where I am using google spreadsheet as a data source. For communication I am using google app script which implements doPost method because my app is sending some data to sheet and also wants some data as a response. The problem is instead of json response I always get html response about redirection in the errorBody().
I have also set OkHttpClient with redirections enabled to my retrofit service, but result is still the same.
I am working with Insomnia rest client for debugging and when I set redirections on there, everything works there fine.
If somebody had the same problem and solved it, please help.
Edit:
Here is my code:
public class Connector {
private static final String BASE_URL = "https://script.googleusercontent.com/";
private static final Object LOCK = new Object();
private static CallTaxiService service;
private static final String TAG = "Connector";
private static CallTaxiService getService()
{
if (service == null)
{
synchronized(LOCK) {
Log.d(TAG, "creating instance");
service = buildService();
}
}
return service;
}
private static CallTaxiService buildService()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(new OkHttpClient.Builder().followRedirects(true)
.followSslRedirects(true).build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(CallTaxiService.class);
}
public static void syncData(List<TaxiServiceAppData> data, Callback<Response> callback)
{
Call<Response> call = getService().sendData(data);
Log.d(TAG, "syncing data");
call.enqueue(callback);
}
private interface CallTaxiService {
#Headers({"Content-type: application/json"})
#POST("endpoint_url")
Call<Response> sendData(#Body List<TaxiServiceAppData> data);
}
}
And here is how I am calling it:
Connector.syncData(taxiServiceAppData, new retrofit2.Callback<com.adrisoft.calltaxi.model.Response>() {
#Override
public void onResponse(Call<com.adrisoft.calltaxi.model.Response> call, Response<com.adrisoft.calltaxi.model.Response> response) {
com.adrisoft.calltaxi.model.Response data = response.body();
if (data != null) {
newCities = data.getCities();
newTaxis = data.getTaxis();
updateDb();
prefs.saveSyncTime();
isSyncRunning = false;
callback.onSuccess();
} else {
try {
Log.d(TAG, "Sync failed ... no data available. Error: " + response.errorBody().string());
} catch (Exception ex) {
}
callback.onFailure();
}
}
#Override
public void onFailure(Call<com.adrisoft.calltaxi.model.Response> call, Throwable t) {
Log.d(TAG, "Sync request failed.");
isSyncRunning = false;
callback.onFailure();
}
});
And exactly in the log "Sync failed ... no data available ..." I am getting this from errorBody():
<HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved here.
</BODY>
</HTML>
Redirect could have happened because the server endpoint provided https and in your code you call http. Then the server would redirect to https. Only GET requests can be redirected, so others like POST will result in error.
I am trying to subscribe events from IBM IoT platform.
The example I tested is from an IBM blog: https://www.ibm.com/developerworks/library/iot-mobile-phone-iot-device-bluemix-apps-trs/. In this link, the codes can be downloaded.
With this example, the Android app can successfully connect and publish events to the IBM IoT platform, but when I tried "subscribe", I always receive the error "Connection lost". What causes this problem? Thank you!
The relevant codes are:
public IMqttToken subscribeToEvent(String deviceType, String deviceId, String event, String format, int qos, Object userContext, IMqttActionListener listener) throws MqttException {
String eventTopic = "iot-2/type/" + deviceType + "/id/" + deviceId + "/evt/" + event + "/fmt/"+format;
return subscribe(eventTopic, qos, userContext, listener);
}
public static void subscribeEvent(Context context,String event) {
Log.v(TAG, ".subscribeEvent() entered")
try {
MyIoTActionListener listener = new MyIoTActionListener(context, Constants.ActionStateStatus.SUBSCRIBE);
IoTClient iotClient = IoTClient.getInstance(context);
String deviceType = "Android";
String deviceId = "...";
iotClient.subscribeToEvent(deviceType, deviceId, event, "json", 0, context,listener);
} catch (MqttException e) {
Log.d(TAG, ".SubscribeEvent() received exception on SubscribeEvent()");
}
}
The connection URL is:
String connectionURI = "tcp://" + this.getOrganization() + ".messaging.internetofthings.ibmcloud.com:1883";
ssl somehow cannot work.
This problem has been solved.
Two concepts have to be clarified: device client and application client.
A device client can only publish events and subscribe commands. In the question, I subscribe events with a device client, the IoT platform cannot response and returns the error "Connection Lost". The correct way to code is:
public IMqttToken subscribeToCommand(String command, String format, int qos, Object userContext, IMqttActionListener listener) throws MqttException {
String commandTopic = getCommandTopic(command, format);
return subscribe(commandTopic, qos, userContext, listener);
}
public static String getCommandTopic(String command, String format) {
return "iot-2/cmd/" + command + "/fmt/json";
}
public static void subscribeCommand(Context context, String command) {
try {
MyIoTActionListener listener = new MyIoTActionListener(context, Constants.ActionStateStatus.SUBSCRIBE);
IoTClient iotClient = IoTClient.getInstance(context);
iotClient.subscribeToCommand(command, "json", 0, context,listener);
} catch (MqttException e) {
Log.d(TAG, ".SubscribeCommand() received exception on SubscribeEvent()");
}
}
The example provided by IBM contains functions like subscribeEvent and publishCommand for a device client. These functions make me confused.
If you want to publish commands and subscribe events, an application client need to be created. The method to create an application client is:
import com.ibm.iotf.client.app.ApplicationClient;
Properties appProperties = new Properties();
appProperties.put("id",applicationId);
appProperties.put("Organization-ID",organizationId);
appProperties.put("Authentication-Method","apikey");
appProperties.put("API-Key",apiKey);
appProperties.put("Authentication-Token",token);
appClient = new ApplicationClient(appProperties);
appClient.connect();
See more about application client at https://github.com/ibm-watson-iot/iot-java/blob/master/src/main/java/com/ibm/iotf/client/app/ApplicationClient.java
I'm running an application with SignalR 2.2.0 on server side and signalr-java-client (self compiled, last GitHub version) on Android as client.
Currently, there are 4 clients connected to my hub. From time to time, it happens, that all 4 clients simultaneously receive the HTTP status 400 with the message "The connection id is in the incorrect format" (the clients were connected before). I analyzed this multiple times and am not able to find any information/pattern when or why this happens.
The connecten is secured via JWT, the token is definitely valid. When retrieving a new token, the connection is stopped and started again. Apart from this, it is very unlikely that the error is device-related, because the error is thrown at all 4 clients the same time.
I know, this error can occur when the client's Identity changes, but an Identity change for 4 clients the same time seems very unlikely to me.
This is the server-code used for authentication (Deepak asked).
The following method gets called in my Startup.cs:
public static void ConfigureOAuth(IAppBuilder app, string audienceID, string sharedSecret)
{
byte[] secret = TextEncodings.Base64Url.Decode(sharedSecret);
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
Provider = new MyOAuthBearerAuthenticationProvider(),
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audienceID },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(Issuer, secret)
}
});
}
Here's the code of MyOAuthBearerAuthenticationProvider class:
class MyOAuthBearerAuthenticationProvider : OAuthBearerAuthenticationProvider
{
/// <summary>
/// Get's a JWT from querysting and puts it to context
/// </summary>
public override Task RequestToken(OAuthRequestTokenContext context)
{
if (context.Token == null)
{
string value = context.Request.Query.Get("auth_token");
if (!string.IsNullOrEmpty(value)) //token from queryString
{
context.Token = value;
}
}
return Task.FromResult<object>(null);
}
}
I have to retrieve the token from query string, because additionally to the java-client, a javascript client is used, which is not able to set headers.
Lastly, I secure my hub and some of it's methods with the Authorization attribute:
[Authorize(Roles = "MyExampleRole")]
This is the client-code for connection:
public boolean connect(String url, String token) {
if (connected) {
return true;
}
try {
this.hubConnection = new HubConnection(url, "auth_token=" + token, true, logger);
this.hubProxy = hubConnection.createHubProxy("MyHub");
this.hubProxy.subscribe(this.signalRMethodProvider);
this.hubConnection.stateChanged(stateChangedCallback);
SignalRFuture<Void> awaitConnection = this.hubConnection.start();
awaitConnection.get(10000, TimeUnit.MILLISECONDS);
return true;
}
catch (InterruptedException | TimeoutException | ExecutionException e) {
log.error("connect", e);
return false;
}
}
Does anybody have an Idea, how to fix this problem or where I may receive further information?
Thank you very much
-Lukas
seems fine...
possible alteration you can do is change
awaitConnection.get(10000, TimeUnit.MILLISECONDS);
to
awaitConnection.done(new Action<Void>() {
#Override
public void run(Void obj) throws Exception {
Log.d(TAG, "Hub Connected");
}
}).onError(new ErrorCallback() {
#Override
public void onError(Throwable error) {
error.printStackTrace();
Log.d(TAG, "SignalRServiceHub Cancelled");
}
}).onCancelled(new Runnable() {
#Override
public void run() {
Log.d(TAG, "SignalRServiceHub Cancelled");
}
});
I'm working on a multi-user Android application that provides to its users access to GMaps (find one another), chat and so on. Users should login to application using their accounts on Twitter, Facebook, Google+ etc. Everything works fine with all accounts except G+ - application can get access to G+ API only with its owner account. With other accounts I receive com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found or "authorization error". App is registered on the API Console, and OAuth2.0 authentication used. I use standard authentication mechanism from Google sites. Is it possible to use different G+ accounts to login?
Here is my code (Android v1.6):
public class GooglePlusActivity extends Activity {
public static final String LOG_TAG = GooglePlusActivity.class.getSimpleName();
public static final String EXTRA_FIRSTNAME = "firstname";
public static final String EXTRA_LASTNAME = "lastname";
public static final String EXTRA_NICKNAME = "nickname";
public static final String EXTRA_SEX = "sex";
public static final String EXTRA_AVATAR = "avatar";
public static final String EXTRA_ID_SOCNET = "id_socnet";
private ApplicationSettings mSettings;
private Person mProfile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSettings = ((TomskApplication)getApplication()).getSettings();
signIn();
}
private void signIn() {
WebView webView = new WebView(this);
setContentView(webView);
webView.getSettings().setJavaScriptEnabled(false);
String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
mSettings.getGPID(), mSettings.getGPRedirectURI(),
mSettings.getGPScope()).build();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (url.startsWith(mSettings.getGPRedirectURI())) {
try {
Intent res_intent = new Intent();
if (url.indexOf("code=") != -1) {
String code = url.substring(mSettings
.getGPRedirectURI().length() + 7, url
.length());
AccessTokenResponse token = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(), mSettings.getGPID(),
mSettings.getGPSecret(), code, mSettings
.getGPRedirectURI()).execute();
mSettings.setGPToken(token);
// Loading user data
retrieveProfile();
if (mProfile == null) {retrieveProfile();}
res_intent.putExtra(EXTRA_FIRSTNAME, mProfile
.getName().getGivenName());
res_intent.putExtra(EXTRA_LASTNAME, mProfile
.getName().getFamilyName());
res_intent.putExtra(EXTRA_NICKNAME,
mProfile.getNickname());
res_intent.putExtra(EXTRA_SEX, mProfile.getGender());
res_intent.putExtra(EXTRA_AVATAR, mProfile
.getImage().getUrl());
res_intent.putExtra(EXTRA_ID_SOCNET, mProfile.getId());
setResult(Activity.RESULT_OK, res_intent);
view.setVisibility(View.INVISIBLE);
finish();
} else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
setResult(Activity.RESULT_CANCELED);
finish();
}
} catch (IOException e) {
Log.d(LOG_TAG, e.toString());
}
return true;
} else {
return false;
}
}
});
webView.loadUrl(googleAuthorizationRequestUrl);
}
/**
* Retrieve user profile
*/
private void retrieveProfile() throws IOException {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();
AccessTokenResponse token = mSettings.getGPToken();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
token.accessToken, transport, jsonFactory,
mSettings.getGPID(), mSettings.getGPSecret(),
token.refreshToken);
Builder b = Plus.builder(transport, jsonFactory)
.setApplicationName("MyApp/1.0");
b.setHttpRequestInitializer(accessProtectedResource);
Plus plus = b.build();
mProfile = plus.people().get("me").execute();
}
}
I've searched on Google sites, Stack Overflow but found nothing. Please help.
Dont know that this will help but, if you are desperate....
New release of some of the Android client side libs on 4/4/2012 here
and there is fresh Google+ sample, using some reconfigured classes in the main() method where they access protected resources. The new version in R 1.8 is different than your code , at least at the top of the stack.... IMO the use in the new example of the Credential class and of the PLUS.Builder is probably going to boil down to pretty much the same implementation that you already have. You may want to look at the newer sample if you cannot get anything else to work.
new code from googlePlus sample in 1.8
public static void main(String[] args) {
try {
try {
// authorization
Credential credential = OAuth2Native.authorize(
HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(),
Arrays.asList(PlusScopes.PLUS_ME));
// set up global Plus instance
plus = Plus.builder(HTTP_TRANSPORT, JSON_FACTORY)
.setApplicationName("Google-PlusSample/1.0").setHttpRequestInitializer(credential)
.build();
older code here