'blogger authentication issues in android
I am working on blogger api and i am using this method to request blogger accesstoken, but problem is if user not select Manage your blog option and continue then how can i force to check this option and when user will give permission then open another activity . Any one who can help me to resolve this issue
public class LoginActivity extends AppCompatActivity {
private final static int RC_SIGN_IN = 2;
GoogleSignInClient mGoogleSignInClient;
GoogleSignInAccount account;
GoogleSignInOptions gso;
private int RC_REQUEST;
String access_token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(BloggerScopes.BLOGGER))
.requestServerAuthCode(getString(R.string.server_client_id))
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
MaterialCardView signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(view -> {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
if (resultCode == Activity.RESULT_OK) {
if (RC_REQUEST == requestCode) {
startActivity(new Intent(LoginActivity.this, BlogActivity.class));
}
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
account = completedTask.getResult(ApiException.class);
String s = account.getServerAuthCode();
requestAccessToken(s);
} catch (ApiException e) {
Toast.makeText(this, "please login", Toast.LENGTH_SHORT).show();
}
}
private void requestAccessToken(String serverAuthCode) {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormEncodingBuilder()
.add("grant_type", "authorization_code")
.add("client_id", getString(R.string.server_client_id))
.add("client_secret", getString(R.string.secret_id))
.add("redirect_uri", "")
.add("code", serverAuthCode)
.build();
final Request request = new Request.Builder()
.url("https://www.googleapis.com/oauth2/v4/token")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(final Request request, final IOException e) {
Log.e("THETOKEN", e.toString());
}
@Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
try {
access_token = jsonObject.getString("access_token");
ACCESS_TOKEN = access_token;
} catch (JSONException e) {
Log.d("", e.toString());
}
} catch (JSONException e) {
Log.d("", e.toString());
}
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|