php - 如何从 Android 应用程序 PHP Web 服务器发布 GoogleAuthUtil.getToken() 的 token

标签 php android oauth google-play-services

我通过以下代码行获得 token :

Bundle appActivities = new Bundle();
    appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
            "http://schemas.google.com/AddActivity");

    String serverClientID = "648976189452-46n3cl4mi4p0vasdr3u1nh87706g0bis.apps.googleusercontent.com";
    String scopes = "oauth2:server:client_id:" + serverClientID
            + ":api_scope:" + Scopes.PLUS_LOGIN;
    String code = null;
    LTDD_1051010005 contex = new LTDD_1051010005();
    try {

        code = GoogleAuthUtil.getToken(contex, // Context context
                plusClient.getAccountName(), // String accountName
                scopes, // String scope
                appActivities // Bundle bundle
                );

    } catch (IOException transientEx) {
        // network or server error, the call is expected to succeed if you
        // try again later.
        // Don't attempt to call again immediately - the request is likely
        // to
        // fail, you'll hit quotas or back-off.
        return transientEx.getMessage();
    } catch (UserRecoverableAuthException e) {
        // Recover
        code = null;
    } catch (GoogleAuthException authEx) {
        // Failure. The call is not expected to ever succeed so it should
        // not be
        // retried.
        return authEx.getMessage();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return code;
}

如何将从我的 Android 应用程序接收到的代码发布到我的 php 网络服务器? 如何验证服务器上的 token 并与 Google 交换访问 token ,之后 php 网络服务器将返回 xml 以及 android 如何立即从服务器接收 xml 数据。

谢谢!

最佳答案

Google+ sign-in documentation 中有关于如何将 token 传递到您的服务器的描述。 .

基本上,您使用 GoogleAuthUtil.getToken,添加服务器的 oauth 客户端 ID 的范围。

String scopes = "oauth2:server:client_id:<SERVER-CLIENT-ID>:api_scope:<SCOPE1> <SCOPE2>";
String code = null;
try {
  code = GoogleAuthUtil.getToken(context, account, scopes, null);
  ...
}

然后您将收到的代码发送到您的服务器,服务器必须用它交换服务器的访问和刷新 token 。

关于php - 如何从 Android 应用程序 PHP Web 服务器发布 GoogleAuthUtil.getToken() 的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368077/

相关文章:

php - 如何在 Laravel 模型上延迟加载自定义属性?

java - 我如何应用alertDialog作为AsyncTask中的服务器响应

php - 为一个查询使用准备好的 mysql 语句是否值得?

android - finish() 和 Activity 生命周期

c# - 如何使用 OAuth 在 C# 中创建新的 Google Drive 服务

javascript - 如何在 Angular 4 应用程序中自动单击按钮?

php - PDO 向 MySQL 数据库中插入一个简单的数组

android - 如何在运行时将 fragment 的屏幕方向锁定为横向

android - AsyncTask<String, Void, String> 通用类型。安卓

node.js - 使用带有 GET 过滤器的 Magento Rest API 时出现 oauth_signature 无效错误