我花了一天的时间才使它成功,所以我认为我的经验可能对某人有用.也许其他一些人会找到改进.
所以我两天前开始使用angularJS.我希望它可以与Google Cloud Endpoints一起使用来创建后端界面.这给我带来了麻烦.
gapi的javascript客户端带有异步加载,因此角度初始化会因为gapi未定义而崩溃.
所以你需要在初始化gapi时引导角度:
<script src="https://apis.google.com/js/client.js?onload=googleOnLoadCallback"></script>添加回调:
function googleOnLoadCallback(){
var apisToLoad = 1; // must match number of calls to gapi.client.load()
var gCallback = function() {
if (--apisToLoad == 0) {
//Manual bootstraping of the application
var $injector = angular.bootstrap(document, ['myApp']);
console.log('Angular bootstrap complete ' + gapi);
};
};
gapi.client.load('helloWorld', 'v1', gCallback, '//' + window.location.host + '/_ah/api');
}
Run Code Online (Sandbox Code Playgroud)感觉很好,但电话怎么样?
所以这是控制器:
angular.module('myApp.controllers', []).
.controller('MyCtrl', ['$scope' ,'helloWorldService',
function($scope,greetingsService) {
helloWorldService.loadData($scope);
}]);
Run Code Online (Sandbox Code Playgroud)
这是服务:
angular.module('myApp.services', [])
service('helloWorldService', [function() { …Run Code Online (Sandbox Code Playgroud) javascript google-app-engine angularjs google-cloud-endpoints gapi
我试图在typescript中从谷歌gapi.auth2导入一些类或函数.但是,即使我在typings目录中正确添加了gapi.auth2类型,代码也永远无法工作.
import { GoogleAuth } from 'gapi.auth2';
我总是有错误:
Error TS2307: Cannot find module 'gapi.auth2'
我应该使用一些相对目录搜索,例如'../../typings/gapi.auth2'?
或者也许我使用gapi的方式是完全错误的?
谢谢!
我正在尝试为我的网站实施Google登录.登录按钮正确显示并且最初签名人员.当我在使用网站后尝试移动到登录页面(我正在使用React,因此它只是一页)时,我的问题就出现了.我使用完全相同的函数来呈现登录页面,但它给了我一个"cb = gapi.loaded_0:249未捕获的TypeError:无法读取属性'样式'的null".gapi中的错误发生在这里(至少我认为):
a.El;window.document.getElementById((c?"not_signed_in":"connected"
Run Code Online (Sandbox Code Playgroud)
这是我最初添加要呈现的登录按钮的方式:
elements.push(h('div.g-signin2',{'data-onsuccess': 'onSignIn'}))
return h('div.page_content',elements)
Run Code Online (Sandbox Code Playgroud)
我后来用ReactDOM.render调用渲染.
以下是我处理SignOut和SignIn的方法:
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
// console.log('User signed out.');
signedin = false;
auth2 = null;
renderPage()
});
}
var google_idtoken;
var signedin = false;
// set auth2 to null to indicate that google api hasn't been loaded yet
var auth2 = null;
function onSignIn(googleUser) {
auth2 = gapi.auth2.getAuthInstance({
client_id: 'ClientID.apps.googleusercontent.com'
});
google_idtoken = googleUser.getAuthResponse().id_token;
wrongemail = true;
// if(auth2 != null && auth2.isSignedIn.get() == …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用gapi.auth2Google Sign-In for Websites API,这是我的代码:
- 加载库:
<script src="https://apis.google.com/js/platform.js?onload=onLoadGapiCallback" async defer></script>
- 初始化auth2变量:
var auth2;
window.onLoadGapiCallback = () => {
gapi.load('auth2', () => {
auth2 = gapi.auth2.init({
'client_id': 'CLIENT_ID',
'scope': 'profile email https://www.googleapis.com/auth/youtube.readonly'
});
});
};
Run Code Online (Sandbox Code Playgroud)
- 点击按钮时:
auth2.signIn().then(() => {
console.log('auth is:', auth2.currentUser.get().getAuthResponse().access_token);
});
Run Code Online (Sandbox Code Playgroud)
这很有效,它初始化auth2变量,当我点击按钮时,它会显示SingIn提示,我选择了一个我的Google帐户.问题是从现在开始我必须选择一个YouTube帐户,如果我选择其他帐户而不是主帐户,我会得到一个像这样的异常对象:
{type: "tokenFailed", idpId: "google", error: "USER_LOGGED_OUT"}
Run Code Online (Sandbox Code Playgroud)
还有一个最后发送的XHR请求有这个响应:
{"error":"USER_LOGGED_OUT","detail":"No active session found."}
Run Code Online (Sandbox Code Playgroud)
所以它只适用于我选择主帐户,但我无法选择其他YouTube帐户.
我在这里错过了什么?我查看了所有这些文档,但没有人帮助我:
更新:从运行该代码示例(但与此scope:'profile email https://www.googleapis.com/auth/youtube.readonly')如果让我选择的第一个YouTube帐户每个谷歌账户才会工作.如果我选择任何其他Youtube帐户,我会收到此警报错误:
jQuery允许以同步(async:false设置)发送http请求,这在绕过弹出窗口阻止程序时特别有用.
我正在尝试通过以下问题使用带有angular2的谷歌登录:Google登录网站和Angular 2使用Typescript
但我收到一个错误:
ORIGINAL EXCEPTION: ReferenceError: gapi is not defined
ORIGINAL STACKTRACE:
ReferenceError: gapi is not defined
at LoginAppComponent.ngAfterViewInit (http://localhost:3000/app/login.component.js:33:9)
at DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.template.js:46:68)
at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12169:23)
at DebugAppView.AppView.detectChangesInternal (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12154:18)
at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12143:18)
at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:12247:48)
at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:10397:69)
at eval (http://localhost:3000/node_modules/@angular/core//bundles/core.umd.js:9911:88)
Run Code Online (Sandbox Code Playgroud)
显然gapi没有定义 - 我可以理解,因为我似乎只是声明一个空的变量.
我目前的代码如下:
import {Component, NgZone} from "@angular/core";
declare var gapi: any;
@Component({
selector: "login",
templateUrl: "templates/login-template.html"
})
export class LoginAppComponent {
googleLoginButtonId = "google-login-button";
userAuthToken = null;
userDisplayName …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照此指南通过Google Api在Google云端硬盘上进行可恢复上传.
这是我的代码,您可以看到它在指南要求时发出2个请求,第一部分创建元数据,然后我们使用该位置开始使用第一个请求创建的会话上载文件.
const file = new File(['Hello, world!'], 'hello world.txt', { type: 'text/plain;charset=utf-8' });
const contentType = file.type || 'application/octet-stream';
const reqMetadata = gapi.client.request({
'path': 'upload/drive/v3/files',
'method': 'POST',
'params': { 'uploadType': 'resumable' },
'headers': {
'X-Upload-Content-Type': file.type,
'X-Upload-Content-Length': file.size,
'Content-Type': 'application/json; charset=UTF-8'
},
'body': {
'name': file.name,
'mimeType': contentType,
'Content-Type': contentType,
'Content-Length': file.size
}
});
reqMetadata.execute((respMetadata, rawRespMetadata: any) => {
const locationUrl = JSON.parse(rawRespMetadata).gapiRequest.data.headers.location;
const reader = new FileReader();
reader.onload = (e) => {
const reqFile = …Run Code Online (Sandbox Code Playgroud) 我是谷歌云数据存储的新手.我目前正在尝试使用我在开发人员控制台创建的API密钥来查询我的数据集.下面是我调用数据存储区API的代码片段(我使用的是gapi.client):
gapi.client.setApiKey('API_KEY');
gapi.client.load('datastore', 'v1beta2').then(function() {
console.log('loaded.');
gapi.client.datastore.datasets.runQuery({'datasetId' : 'myProjectId','gqlQuery': { 'queryString': 'select * from locationReport'}}).then(function(resp) {
console.log(resp.result);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);}
);
});
Run Code Online (Sandbox Code Playgroud)
这回复了以下回复:
响应标头:
HTTP/1.1 401 Unauthorized
Vary: Origin
Vary: X-Origin
WWW-Authenticate: Bearer realm="https://accounts.google.com/"
Content-Type: text/html; charset=UTF-8
Content-Encoding: gzip
Date: Mon, 22 Jun 2015 14:49:06 GMT
Expires: Mon, 22 Jun 2015 14:49:06 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=1
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
回应机构:
要求登录
虽然这里提到可以使用公共API访问数据,但我仍然无法这样做.
请帮我解决一下这个.让我知道是否可能或是否有任何其他方法从数据存储区检索数据而无需OAuth等用户权限
<html>
<head>
<meta name="google-signin-client_id" content="MY_CLIENT_ID.apps.googleusercontent.com" />
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script type="text/javascript">
function onSignIn(googleUser) {
console.log("Succesfully Singed in!!!");
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
通过上面我可以看到一个谷歌登录按钮打开一个弹出窗口接受凭证并签到我.但我希望登录成功时触发onSignIn方法,这是不会发生的.有没有人对此有任何指示?
通过safari,chrome和firefox浏览器在osx上的apache服务器上试用它
要在firebase auth登录后获取Google访问令牌,我知道我可以这样做:
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果用户已经过身份验证并且我需要令牌呢?有没有办法从Firebase auth中提取它?
我已经完成了所有的价值,authState但我找不到我一直在寻找的谷歌访问令牌.
gapi ×10
javascript ×7
google-api ×3
angular ×2
typescript ×2
angularjs ×1
firebase ×1
reactjs ×1
youtube-api ×1