我正在使用Cordova开发一个应用程序,并希望在Googe Drive中保存文件 .
我使用cordova-plugin-googleplus(https://github.com/EddyVerbruggen/cordova-plugin-googleplus)成功登录Google . 但是我无法让插件返回给我accessToken或idToken所以我可以使用谷歌JavaScript API .
window.plugins.googleplus.login(
{
'scopes': 'https://www.googleapis.com/auth/drive.file profile',
'offline': true,
'webApiKey': ‘CODE’
},
function (obj) {
$scope.$apply(function() {
$scope.srcImage = obj.imageUrl;
$scope.NomeGoogle = obj.displayName;
});
},
function (msg) {
alert('Erro');
alert('error: ' + msg);
}
);
我尝试使用下面的代码,但返回给我以下错误:
“Uncaught gapi.auth2.ExternallyVisibleError:无效的cookiePolicy”
gapi.load('auth2', function() {
gapi.auth2.init({
client_id: 'REVERSED_CLIENTID',
}).then(function(){
auth2 = gapi.auth2.getAuthInstance();
console.log(auth2.isSignedIn.get()); //now this always returns correctly
});
});
3 回答
我设法找出问题,为什么不从插件获取serverAuthCode . 必须在Google Developers Console上创建2个凭据 . 第一个必须是Android,这将是插件而第二个应该是Web App,这是实现serverAuthCode所必需的 .
代码看起来像这样
谢谢你的回复rojobo
起初我希望跳过对cordova-plugin-googleplus的需求,只需使用Cordova / PhoneGap中的
gapi
来处理与Google的身份验证,但它似乎是gapi client authentication may not work within cordova's file:// protocol .answer from @Joao向我发送了正确的方向,但在检索
access_token
后尝试使用gapi
时,我一直收到Invalid cookiePolicy
错误(这是因为我忽略了下面列出的第2步,并且在使用plugini进行身份验证后我错误地尝试了再次使用_3026919进行身份验证 .有一个步骤(下面提到的#3)我不清楚 . 要通过Google进行身份验证,然后在Cordova / PhoneGap中使用
gapi
,这样就可以了......使用cordova-plugin-googleplus来处理身份验证和访问令牌检索,不要在这里使用gapi
加载gapi客户端库(跳过
gapi.client.init()
调用和所有正常的gapi身份验证过程)将我们从插件中获取的访问令牌附加到gapi客户端,然后根据需要进行gapi调用
第3步采取some digging for me to find,意味着我需要添加
access_token
一旦访问令牌附加到gapi客户端,我就可以使用Cordova / Phonegap中的gapi:
尝试