【问题标题】:Invalid Accountmanager auth token无效的 Accountmanager 身份验证令牌
【发布时间】:2012-08-01 22:19:40
【问题描述】:

您好,我正在尝试从我的 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;
}

所以我得到了一个令牌,然后我将它发送到我的 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);  
}
?>

我不断收到错误:401 无效令牌:S

有没有人有解决方案或好的样本(找不到与我想做的匹配的!)

【问题讨论】:

    标签: android webserver accountmanager auth-token


    【解决方案1】:

    我能想到你可能遇到的两个问题:

    1. 授权令牌已过期。当您调用方法updateToken() 来获取令牌时,您需要将参数invalidateToken 设置为true 以确保您获得新令牌。
    2. SCOPE 的值有误。根据您提供的代码,您似乎正在尝试使用 Google 联系人 API,因为此 API SCOPE 的值应为 https://www.google.com/m8/feeds

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 2012-06-03
      • 2014-11-04
      • 2020-11-22
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2018-11-26
      相关资源
      最近更新 更多