答案 0 :(得分:4)
回答关于oauth范围的疑问(仅对googlers有用):
要完全理解,谷歌一些关于身份验证和授权的概念。
检查用户/密码是否与身份验证部分有关。
授权部分需要范围:您有权代表用户执行或接收的内容。要获取允许的范围列表,请查看OAuth服务文档。
从Google和G +可以找到最常见的范围:https://developers.google.com/+/api/oauth?hl=pt-ZA
例如,要从用户获取所有可能的信息,您可以使用范围:
“openid个人资料电子邮件https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me”
(第一个单词指的是协议,后跟要求响应字段的单词,并且可以使用空格分隔符声明所需的范围)
注意:稍后,如果您尝试使用访问令牌来请求或执行之前未向范围询问的任何内容,则该服务可能会返回授权错误。
对于Google,您可以用来了解其OAuth服务和范围的好工具是OAuth Playground:https://developers.google.com/oauthplayground/
答案 1 :(得分:3)
你看过API reference吗?
您可能正在寻找的课程是com.google.android.gms.auth.GoogleAuthUtil
。
除其他外,它提供以下方法:
static String getToken(Context context, String accountName, String
说明强>
对用户进行身份验证并返回有效的Google身份验证令牌,如果获取令牌时出错,则会抛出异常。
<强>用法:强>
String token;
try {
token = GoogleAuthUtil.getToken(context, accountName, scope);
} catch (GooglePlayServicesAvailabilityException playEx) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
Activity.this,
AUTH_REQUEST_CODE);
// Use the dialog to present to the user.
} catch (UserRecoverableAutException recoverableException) {
Intent recoveryIntent = recoverableException.getIntent();
// Use the intent in a custom dialog or just startActivityForResult.
} catch (GoogleAuthException authEx) {
// This is likely unrecoverable.
Log.e(TAG, "Unrecoverable authentication exception: " + authEx.getMesssage(), authEx);
} catch (IOException ioEx) {
Log.i(TAG, "transient error encountered: " + ioEx.getMessage());
doExponentialBackoff();
}
答案 2 :(得分:0)
您需要使用异步任务获取它。
public function login_user() {
$returnStr ['status_code'] = 0;
$returnStr ['message'] = 'welcome';
//print_r($_POST);die;
$email = $this->input->post ( 'email' );
$pwd = md5 ( $this->input->post ( 'password' ) );
$bpath = $this->input->post ('bpath');
$remember = $this->input->post ( 'remember' );
if (valid_email($email)) {
$condition = array (
'email' => $email,
'password' => $pwd,
'status' => 'Active'
);
$checkUser = $this->user_model->get_all_details ( USERS, $condition );
if ($checkUser->num_rows () == '1') {
$userdata = array (
'fc_session_user_id' => $checkUser->row ()->id,
'session_user_email' => $checkUser->row ()->email
);
$this->session->set_userdata ( $userdata );
$datestring = "%Y-%m-%d %h:%i:%s";
$time = time ();
$newdata = array (
'last_login_date' => mdate ( $datestring, $time ),
'last_login_ip' => $this->input->ip_address () ,
'login_hit' => 0
);
$condition = array (
'id' => $checkUser->row ()->id
);
$this->user_model->update_details ( USERS, $newdata, $condition );
if ($remember != '') {
$userid = $this->encrypt->encode ( $checkUser->row ()->id );
$cookie = array (
'name' => 'admin_session',
'value' => $userid,
'expire' => 86400,
'secure' => FALSE
);
$this->input->set_cookie ( $cookie );
}
$this->setErrorMessage ( 'success', 'You are Logged In ... !' );
$returnStr ['status_code'] = 1;
}
else
{
$condition = array (
'email' => $email,
'status' => 'Active'
);
$checkUser = $this->user_model->get_all_details ( USERS, $condition );
$login_hit = 0;
if ($checkUser->num_rows () == '1')
{
$login_hit = $checkUser->row()->login_hit;
$login_hit = $login_hit+1;
$newdata = array (
'login_hit' => $login_hit
);
$condition = array (
'id' => $checkUser->row ()->id
);
$this->user_model->update_details ( USERS, $newdata, $condition );
}
if($login_hit < 5)
{
$returnStr ['message'] = 'Invalid login details';
}
else
{
$pwd = $this->get_rand_str ( '6' );
$newdata = array (
'password' => md5 ( $pwd )
);
$condition = array (
'email' => $email
);
$this->user_model->update_details ( USERS, $newdata, $condition );
$this->send_user_password ( $pwd, $checkUser );
$this->setErrorMessage ( 'success', 'New password sent to your mail' );
$returnStr ['message'] = 'New password sent to your email';
$returnStr ['status_code'] = 1;
}
}
} else {
$returnStr ['message'] = "Invalid email id";
}
echo json_encode ( $returnStr );
}
您的访问令牌将存储到令牌变量中。