我一直在尝试找出与我的Google Drive Picker集成不正确的地方,但未能做到。
选择器打开后,将显示“登录”屏幕,然后将首次打开OAuth流程。完成后,它将再次重复“登录”屏幕。这次再次单击它时,显示错误-“您请求的功能当前不可用。请稍后再试。”
我尝试搜索各种答案,但没有一个奏效。我已经尝试在选择器初始化之前添加OAuth流,但仍然不能解决问题。任何帮助将不胜感激。
import { h, Component } from 'preact';
import googleClientAPI from 'google-client-api';
import styles from 'stylesheets/components/commons/drive';
import config from '../../config';
export default class DriveFilePicker extends Component {
constructor(props) {
super(props);
this.setState({
loaded: false,
});
}
componentWillMount() {
googleClientAPI().then(gapi => {
gapi.load('picker', this.buildPicker);
});
}
handleAuthResult = (authResult) => {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
this.buildPicker(oauthToken);
}
}
buildPicker = (accessToken) => {
accessToken = accessToken || this.props.accessToken;
if (typeof google !== 'undefined' && typeof accessToken !== 'undefined') {
const docsView = new google.picker.DocsView()
.setIncludeFolders(true)
.setMimeTypes('application/vnd.google-apps.folder')
.setSelectFolderEnabled(true);
this.picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.addView(docsView)
.setOAuthToken(accessToken)
.setDeveloperKey(config.gapi.developerKey)
.setCallback(this.onPicked)
.build();
this.setState({
loaded: true,
});
}
}
componentWillReceiveProps({ accessToken: nextAccessToken }) {
if (this.props.accessToken !== nextAccessToken) {
this.setState({
loaded: false,
});
this.buildPicker(nextAccessToken);
}
}
onPicked = ({ action, docs }) => {
if (action === 'picked') {
this.props.onAddFolder(docs);
}
}
onOpen() {
this.state.loaded && this.picker.setVisible(true);
}
render() {
const { loaded } = this.state;
return (
<div className={`${!loaded ? styles.disabled : ''} ${styles.drive}`}>
<div className={styles.label}>Sync folders</div>
<div className={`${!loaded ? styles.disabled : ''} ${styles.folders}`}>
<Tag hideRemove={true} onClick={::this.onOpen}>+Add folder</Tag>
</div>
</div>
);
}
}
答案 0 :(得分:0)
最后获得了这一系列函数来加载api
platform
和client
,我在<script src/>
中也有index.html
,但是我认为那不是安装后appendChild
script
时需要。将api
加载到platform
到client:auth2
,然后从picker
成功authResult
构建authorize
的顺序似乎很重要。我无法使用sign-in2
+ init
,所以我使用了authorize
var SCOPES = "https://www.googleapis.com/auth/drive.file"
...
componentDidMount = () => {
this.loadGoogleAPI()
}
加载Google脚本
loadGoogleApi() {
const script = document.createElement("script");
script.src = "https://apis.google.com/js/api.js";
script.onload = () => {
console.log("google api loaded");
this.loadGapi();
};
document.body.appendChild(script);
}
loadGapi = () => {
const script = document.createElement("script");
script.src = "https://apis.google.com/js/platform.js";
script.onload = () => {
console.log("google api platform loaded");
this.loadGapiClient();
};
document.body.appendChild(script);
};
loadGapiClient = () => {
const script = document.createElement("script");
script.src = "https://apis.google.com/js/client.js";
script.onload = () => {
console.log("google api client loaded");
/*this.gapi.client.load("youtube", "v3", () =>
console.log("loaded youtube")
);*/
this.gapi = window.gapi;
this.gapi.load("client:auth2", () => {
/*this.gapi.load("signin2", () => {
console.log("render a sign in button");
// using this method will show Signed In if the user is already signed in
});*/
console.log("auth2 loaded");
});
};
document.body.appendChild(script);
};
点击了Google品牌的登录按钮
loadGapiApi = () => {
//var DIALOG_DIMENSIONS = { width: "90%", height: "90%" };
this.gapi = window.gapi;
console.log("authooo");
this.gapi.auth2.authorize(
//getAuthInstance().signIn(
//init(
//authorize(
{
api_key: API_KEY,
client_id: CLIENT_ID,
scope: SCOPES,
prompt: "none"
//prompt: "select_account"
},
(authResult) => {
if (authResult) {
this.gapi.client.load("drive", "v3", () =>
console.log("loaded drive")
);
this.gapi.load("picker", () => {
console.log("drive picker loaded");
this.googlepicker = window.google;
var view1 = new this.googlepicker.picker.DocsView(
this.googlepicker.picker.ViewId.IMAGE
).setSelectFolderEnabled(true);
var view = new this.googlepicker.picker.DocsView(
this.googlepicker.picker.ViewId.DOCS
).setSelectFolderEnabled(true);
var uploadView = new this.googlepicker.picker.DocsUploadView();
view.setMode(this.googlepicker.picker.DocsViewMode.LIST);
view.setQuery(window.dataFeedName);
this.picker = new this.googlepicker.picker.PickerBuilder()
.setAppId(CLIENT_ID)
.setOAuthToken(authResult.access_token)
.addView(view)
.addView(uploadView)
//.enableFeature(google.picker.Feature.SIMPLE_UPLOAD_ENABLED)
//.enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)
.enableFeature(
this.googlepicker.picker.Feature.MULTISELECT_ENABLED
) //optional
.setOrigin("https://thumbprint.us")
.setCallback(this.pickerCallback)
.build();
window.picker = this.picker;
});
this.gapi.load("drive-share", () => {
console.log("share dialog loaded");
this.s = window.s;
this.s = new window.gapi.drive.share.ShareClient();
this.s.setOAuthToken(authResult.access_token);
});
this.setState({
authResult,
accessToken: authResult.access_token,
authorizedScopes: true,
signedIn: true
});
//const auth2 = this.gapi.auth2.getAuthInstance();
//this.auth2.signIn();
}
}
);
};
共享驱动器和注销
logout = () => {
window.location.href =
"https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://thumbprint.us";
};
signOut = () => {
console.log("trying to sign out");
if (this.gapi) {
//console.log(auth2);
this.auth2 = window.auth2;
this.auth2
.signOut()
.then(() => {
//if (auth2 !== null) {
console.log("outted successfully");
this.auth2.disconnect();
})
.catch((err) => console.log(err.message));
this.auth2.disconnect();
//this.logout();
}
};
switchAccount = () => {
console.log("trying to sign out");
this.gapi = window.gapi;
this.gapi.auth2
.getAuthInstance()
.signIn({
api_key: API_KEY,
client_id: CLIENT_ID,
scope: SCOPES,
prompt: "select_account"
})
.then(() => {
//if (auth2 !== null) {
console.log("outted successfully");
this.auth2.disconnect();
})
.catch((err) => console.log(err.message));
};
pickerCallback = (data) => {
if (data.action === window.google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
//console.log(fileId);
//console.log(window.gapi);
//console.log(data.docs[0].name);
this.s.setItemIds([fileId]);
if (this.state.filesPreparedToSend !== data.docs) {
//console.log(data.docs);
this.setState({ filesPreparedToSend: data.docs });
}
}
};