我使用google plus signin构建一个Android应用程序。登录后,mainActivity获取令牌代码并将其发送到php服务器,我的php服务器将使用代码验证并从谷歌获取访问令牌。如果成功,php服务器会在会话中存储访问令牌,并允许android客户端将数据从服务器加载到android app。
但我的php-googe-library存在问题:
在Android应用程序中,我使用以下代码获取令牌:
String token = null;
Bundle appActivities = new Bundle();
appActivities.putString(
GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
Constants.ADD_ACTIVITY_SCHEME);
String serverClientID = "MY_CLIENT_ID";
String scopes = "oauth2:server:client_id:" + serverClientID
+ ":api_scope:" + Scopes.PLUS_LOGIN;
// String scope = "oauth2:" + Scopes.PLUS_LOGIN;
try {
token = GoogleAuthUtil.getToken(MainActivity.this,// Context
mPlusClient.getAccountName(), // String accountName
scopes, // String scope
appActivities // Bundle bundle
);
Log.i("TAG", "token" + token);
} catch (IOException transientEx) {
} catch (UserRecoverableAuthException e) {
Intent recover = e.getIntent();
startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
Log.w("Loi xay ra NeedPermission", e.getMessage());
} catch (GoogleAuthException authEx) {
}
return token;
收到令牌后,我将它发布到我的php服务器上使用此方法:
public static String postTokenToWebServer(String url, String data) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("code", data));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStream is = resEntity.getContent();
String result = StringUtil.convertStreamToString(is);
return result;
} catch (ClientProtocolException e) {
return "ClientProtocolException";
} catch (IOException e) {
return "IOException";
}
}
我在php服务器上收到了代码。我调用google客户端的身份验证($ code)方法来验证访问令牌的代码,请使用followcodé:
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_PlusService.php";
$client = new Google_Client();
$client->setClientId('MY_CLIENT_ID');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->setRedirectUri('postmessage');
$client->setDeveloperKey('MY_DEVELOPER_KEY');
$client->setScopes('https://www.googleapis.com/auth/plus.login');
if (isset($_POST['code'])) {
session_start();
$mToken = $_POST['code'];
echo "Token code is: $mToken";
$client->authenticate($_POST['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
但是,我收到一个错误: invalid_request origin参数是必需的!,整个是:
Lỗi:invalid_request
origin parameter is required!
Tìm hiểu thêm
Chi tiết yêu cầu
response_type=code
scope=https://www.googleapis.com/auth/plus.login
redirect_uri=postmessage
access_type=offline
approval_prompt=force
client_id=648976189452-lgujem37fatd8cnlrn1uicp81q0tgfn7.apps.googleusercontent.com
我已阅读并尝试了许多方法来解决此问题,但仍无效。我该怎么办呢。帮助