简体   繁体   English

无效的Accountmanager身份验证令牌

[英]Invalid Accountmanager auth token

Hello I'm trying to validate the token I've created with the accountManager from my php server but I keep getting the error "invalid token" from google server on my php server... Here is the code : 您好,我正在尝试从我的PHP服务器上验证使用accountManager创建的令牌,但是我一直从我的PHP服务器上的Google服务器上收到错误“无效令牌” ...这是代码:

private String updateToken(boolean invalidateToken, int accountref) {
    String authToken = "null";
    try {
        AccountManager am = AccountManager.get(TestAuthActivity.this);
        Account[] accounts = am.getAccountsByType("com.google");
        AccountManagerFuture<Bundle> accountManagerFuture;
        if(TestAuthActivity.this == null){//this is used when calling from an interval thread
            accountManagerFuture = am.getAuthToken(accounts[accountref], SCOPE, false, null, null);
        } else {
            accountManagerFuture = am.getAuthToken(accounts[accountref], SCOPE, null, TestAuthActivity.this, null, null);
        }
        Bundle authTokenBundle = accountManagerFuture.getResult();
        authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
        if(invalidateToken) {
            am.invalidateAuthToken("com.google", authToken);
            authToken = updateToken(false, accountref);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Dialog d = new Dialog(TestAuthActivity.this);
    d.setTitle("Token :" + authToken);
    d.show();

    createSession(TestAuthActivity.this, authToken);

    return authToken;
}

So I'm getting a token then I'm sending it to my php server : 所以我得到一个令牌,然后将其发送到我的php服务器:

<?php

if( isset($_POST['authToken'])){        

    //pour que la réponse s'affiche comme du texte brut
    header('Content-Type: text/plain');

    /*partie à modifier*/
    $name = 'www.google.com';//nom du site

    $data = $_POST['authToken'];

    $envoi  = "POST /m8/feeds/contacts/default/full HTTP/1.1\r\n";
    $envoi .= "Host: ".$name."\r\n";
    $envoi .= "Authorization: GoogleLogin auth='".$data."'\r\n";
    $envoi .= "Connection: Close\r\n";
    $envoi .= "Content-type: application/x-www-form-urlencoded\r\n";
    $envoi .= "Content-Length: ".strlen($data)."\r\n\r\n";
    $envoi .= $data."\r\n";

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if($socket < 0){
            die('FATAL ERROR: socket_create() : " '.socket_strerror($socket).' "');
    }

    if (socket_connect($socket,gethostbyname($name),80) < 0){
            die('FATAL ERROR: socket_connect()');
    }

    if(($int = socket_write($socket, $envoi, strlen($envoi))) === false){
            die('FATAL ERROR: socket_write() failed, '.$int.' characters written');
    }

    $reception = '';
    while($buff = socket_read($socket, 2000)){
       $reception.=$buff;
    }
    echo(json_encode($reception));

    socket_close($socket);  
}
?>

and I keep getting the error : 401 invalid token :S 而且我不断收到错误:401无效令牌:S

Does anyone have a solution or a good sample (couldn't found one that matches what I want to do !) 有没有人有解决方案或好的样本(找不到与我想要的样本匹配的样本!)

I can think of two possible problems you might have: 我可以想到您可能遇到的两个可能的问题:

  1. The auth token has expired. 身份验证令牌已过期。 When you call your method updateToken() to get the token you need to set the parameter invalidateToken to true to make sure you get a fresh token. 当您调用方法updateToken()来获取令牌时,需要将参数invalidateToken设置为true以确保获得了新令牌。
  2. The value of SCOPE is wrong. SCOPE的值是错误的。 Based on the code you've provided it looks like you are trying to use the Google Contacts API, for this API SCOPE should have the value https://www.google.com/m8/feeds 根据您提供的代码,您似乎正在尝试使用Google Contacts API,因此该API SCOPE的值应为https://www.google.com/m8/feeds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android 2.3上运行时,AccountManager返回无效的auth_token - AccountManager returns invalid auth_token when run on android 2.3 使用CountdownLatch从accountmanager获取Auth令牌 - Getting an Auth Token from accountmanager using a CountdownLatch Accountmanager Auth令牌没有clientID和clientSecret - Accountmanager Auth token whithout clientID and clientSecret 具有多种身份验证令牌类型的Android AccountManager - Android AccountManager with multiple auth token types 从AccountManager获取基本的Google身份验证令牌 - Obtaining a basic google auth-token from AccountManager 如何使用AccountManager为多个服务请求身份验证令牌? - How to request an auth token for multiple services using AccountManager? 如何在AccountManager中存储和检索auth_token - How to store and retrieve auth_token to/from AccountManager 从Android中的Accountmanager获取Dropbox帐户的身份验证令牌 - Getting auth token for dropbox account from accountmanager in android 是否可以从AccountManager获取身份验证令牌而不触发“权限请求”对话框? - Is it possible to get an auth token from AccountManager without triggering the “Permission request” dialog? accountmanager刷新令牌(离线访问) - accountmanager refresh token(offline access)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM