0

こんにちは、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

誰かが解決策または良いサンプルを持っていますか (私がやりたいことに一致するものが見つかりませんでした!)

4

1 に答える 1

1

考えられる問題として、次の 2 つが考えられます。

  1. 認証トークンの有効期限が切れています。メソッドを呼び出してupdateToken()トークンを取得するときは、パラメーターinvalidateTokenを true に設定して、新しいトークンを確実に取得する必要があります。
  2. の値SCOPEが間違っています。提供したコードに基づいて、Google Contacts API を使用しようとしているように見えます。この APISCOPEの値はhttps://www.google.com/m8/feedsである必要があります。
于 2012-11-27T09:50:16.443 に答える