Google登录API令牌到期

时间:2016-01-15 12:10:15

标签: android google-signin

我在我的Android应用中使用com.google.android.gms:play-services:8.4.0。在当前版本中,我只计划没有后端服务器的本地身份验证。我已根据example实施了登录。它可以工作,但在一段随机的时间内,用户会自动注销。并且用户必须再次登录。我想避免自动注销。我怀疑生成的令牌已经过期,应该刷新,但遗憾的是我找不到任何参考如何在Android的Google文档中做到这一点。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

抱歉,无法在线发表评论。但是,“用户是否自动注销”是什么意思?

内部存在隐藏的令牌管理,但令牌将在到期时自动刷新。

假设您已经完成了登录流程。重新打开应用后,代码的行为应如下所示:

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        // This code path should be reached if your last (refreshed) sign-in happens
        // within a short period of time
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                // This code path should be reached if your last (refreshed)
                // sign-in is a while back and token needs to be refreshed. But you
                // should still be able to get a successful googleSignInResult back.
                handleSignInResult(googleSignInResult);
            }
        });
    }

您可以粘贴代码吗?

答案 1 :(得分:0)

我建议使用与伊莎贝拉提到的相同的 public static void doAddCalendarEvent(MyEvent myEvent, Activity activity, int calendarId) { try { if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { return; } String desc = myEvent.getDesc(); desc += "\n\n Added by MyApp"; ContentResolver cr = activity.getContentResolver(); ContentValues values = new ContentValues(); values.put(Events.DTSTART, myEvent.getEventStartTime()); values.put(Events.DTEND, myEvent.getEventEndTime()); values.put(Events.TITLE, myEvent.getTitle()); values.put(Events.DESCRIPTION, desc); values.put(Events.CALENDAR_ID, calendarId); values.put(Events.EVENT_LOCATION, myEvent.getLocation()); values.put(Events.STATUS, Events.STATUS_CONFIRMED); values.put(Events.AVAILABILITY, Events.AVAILABILITY_BUSY); if (hasAlarm) { values.put(Events.HAS_ALARM, true); } values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); long eventId = 0; Uri uri = cr.insert(Events.CONTENT_URI, values); if (uri != null) { eventId = Long.parseLong(uri.getLastPathSegment()); Log.v(TAG, "INSERTED EVENT ID=>" + eventId + "--->" + startDate); } if (hasAlarm) { setReminder(cr, reminderValue, eventId); } } catch (Exception e) { e.printStackTrace(); } } ,尽管有一些变化。我无法让谷歌建议实施SilentSignIn的工作方式(这是伊莎贝拉发布的代码)。相反,我将SilentSignIn移到其封闭的if语句之上,然后删除了if语句本身。

您可以在此处查看我更完整的答案:https://stackoverflow.com/a/43059487/4561506