Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Google Drive API for DotNet中的Gmail用户/密码验证_Asp.net - Fatal编程技术网

Asp.net Google Drive API for DotNet中的Gmail用户/密码验证

Asp.net Google Drive API for DotNet中的Gmail用户/密码验证,asp.net,Asp.net,我使用GoogleDriveAPI从Asp.net上传文档到驱动器。工作正常,但我需要使用ClientID和ClientSecret进行身份验证。不是gmail用户名/密码。在进行授权身份验证后,登录后会重定向到gmail进行登录,这是在请求驱动器的AccessPermission。因此,我如何传递用于身份验证的gmail用户名和密码,而不是ClientID和ClientSecret。之后,我可以再次限制登录进行授权 using System; using System.Coll

我使用GoogleDriveAPI从Asp.net上传文档到驱动器。工作正常,但我需要使用ClientID和ClientSecret进行身份验证。不是gmail用户名/密码。在进行授权身份验证后,登录后会重定向到gmail进行登录,这是在请求驱动器的AccessPermission。因此,我如何传递用于身份验证的gmail用户名和密码,而不是ClientID和ClientSecret。之后,我可以再次限制登录进行授权

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DotNetOpenAuth.OAuth2;
    using Google.Apis.Authentication.OAuth2;
    using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
    using Google.Apis.Drive.v2;
    using Google.Apis.Drive.v2.Data;
    using Google.Apis.Util;
    using System.Diagnostics;
    using DotNetOpenAuth.Messaging;
    using System.Text;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {

      private static DriveService driveService;
      private static OAuth2Authenticator<WebServerClient> authenticator;
      private static IAuthorizationState _state;

      String CLIENT_ID = "90994017777728.apps.googleusercontent.com";
      String CLIENT_SECRET = "IR777777773Nf3vnLT0v";

      private static readonly string[] SCOPES = new[] { DriveService.Scopes.Drive.GetStringValue() };

        protected void Page_Load(object sender, EventArgs e)
        {
          Login();
        }

        private IAuthorizationState AuthState {
          get {
            return _state ?? HttpContext.Current.Session["GoogleAuthState"] as IAuthorizationState;
          }
        }

        private OAuth2Authenticator<WebServerClient> CreateAuthenticator() {
          var provider = new WebServerClient(GoogleAuthenticationServer.Description,CLIENT_ID,CLIENT_SECRET);
          var authenticator = new OAuth2Authenticator<WebServerClient>(provider,GetAuthorization);
          return authenticator;
        }

        private IAuthorizationState GetAuthorization(WebServerClient client) {
          IAuthorizationState state = AuthState;
          if (state != null) {
            if (state.AccessTokenExpirationUtc.Value.CompareTo(DateTime.Now.ToUniversalTime()) > 0)
              return state;
            else
              state = null;
          }
          state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
          if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken))) {
            if (state.RefreshToken == null)
              state.RefreshToken = "";
            HttpContext.Current.Session["GoogleAuthState"] = _state = state;
            return state;
          }
          client.RequestUserAuthorization(SCOPES,"",HttpContext.Current.Request.Url); // Redirecting to Gmail LoginPage
          return null;
        }

        private string GetContentType(string fileExtension) {
          if (string.IsNullOrEmpty(fileExtension))
            return string.Empty;

          string contentType = string.Empty;
          switch (fileExtension) {
            case "htm":
            case "html":
              contentType = "text/HTML";
              break;

            case "txt":
              contentType = "text/plain";
              break;

            case "doc":
            case "rtf":
            case "docx":
              contentType = "Application/msword";
              break;

            case "xls":
            case "xlsx":
              contentType = "Application/x-msexcel";
              break;

            case "jpg":
            case "jpeg":
              contentType = "image/jpeg";
              break;
            case "png":
              contentType = "image/png";
              break;
            case "bmp":
              contentType = "image/bmp";
              break;
            case "gif":
              contentType = "image/GIF";
              break;

            case "pdf":
              contentType = "application/pdf";
              break;
          }

          return contentType;
        }

        public void Login() {
          if (authenticator == null)
            authenticator = CreateAuthenticator();
          if (driveService == null) {
            driveService = new DriveService(authenticator);
          }

        }

        protected void Button1_Click(object sender,EventArgs e) {
          string[] stringParts = FileUpload1.FileName.Split(new char[] { '/' });
          string str = stringParts[stringParts.Length - 1].ToString();
          string[] strType = str.Split(new char[] { '.' });
          string type = GetContentType(strType[1]);
          Google.Apis.Drive.v2.Data.File newFile = new Google.Apis.Drive.v2.Data.File { Title = FileUpload1.FileName,MimeType = type };
          byte[] byteArray = FileUpload1.FileBytes;
          MemoryStream stream = new MemoryStream(byteArray);
          FilesResource.InsertMediaUpload request = driveService.Files.Insert(newFile,stream,newFile.MimeType);
          request.Convert = true;
          request.Upload();
          Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
          InsertPermission(driveService,file);
          HttpContext.Current.Session["GoogleAuthState"] = null;
          Response.Write(file.AlternateLink);
        }


        public void InsertPermission(DriveService service,Google.Apis.Drive.v2.Data.File file) {
          Permission newPermission = new Permission();
          newPermission.Value = TextBox1.Text;
          newPermission.Type = "user";
          newPermission.Role = "reader";
          try {
            Permission per = service.Permissions.Insert(newPermission,file.Id).Fetch();
          }
          catch (Exception e) {
            Console.WriteLine("An error occurred: " + e.Message);
          }
        }
使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用DotNetOpenAuth.OAuth2;
使用Google.api.Authentication.OAuth2;
使用Google.api.Authentication.OAuth2.DotNetOpenAuth;
使用Google.api.Drive.v2;
使用Google.api.Drive.v2.Data;
使用Google.api.Util;
使用系统诊断;
使用DotNetOpenAuth.Messaging;
使用系统文本;
使用System.IO;
公共部分类\u默认值:System.Web.UI.Page
{
专用静态驱动服务驱动服务;
私有静态OAuth2Authenticator验证器;
私有静态IAuthorizationState\u状态;
String CLIENT_ID=“90994017777728.apps.googleusercontent.com”;
字符串CLIENT_SECRET=“ir773nf3nvnlt0v”;
私有静态只读字符串[]SCOPES=new[]{DriveService.SCOPES.Drive.GetStringValue()};
受保护的无效页面加载(对象发送方、事件参数e)
{
登录();
}
私有IAAuthorizationState AuthState{
得到{
返回_state??HttpContext.Current.Session[“GoogleAuthState”]作为IAAuthorizationState;
}
}
专用OAuth2Authenticator CreateAuthenticator(){
var provider=新的WebServerClient(GoogleAuthenticationServer.Description,CLIENT\u ID,CLIENT\u SECRET);
var authenticator=新的OAuth2Authenticator(提供者,GetAuthorization);
返回验证器;
}
私有IAAuthorizationState GetAuthorization(WebServerClient客户端){
IAAuthorizationState=AuthState;
如果(状态!=null){
if(state.AccessTokenExpirationUtc.Value.CompareTo(DateTime.Now.ToUniversalTime())>0)
返回状态;
其他的
state=null;
}
state=client.ProcessUserAuthorization(新的HttpRequestInfo(HttpContext.Current.Request));
if(state!=null&(!string.IsNullOrEmpty(state.AccessToken)| |!string.IsNullOrEmpty(state.refreshttoken))){
if(state.RefreshToken==null)
state.RefreshToken=“”;
HttpContext.Current.Session[“GoogleAuthState”]=\u state=state;
返回状态;
}
client.RequestUserAuthorization(SCOPES,“,HttpContext.Current.Request.Url);//重定向到Gmail登录页面
返回null;
}
私有字符串GetContentType(字符串文件扩展名){
if(string.IsNullOrEmpty(文件扩展名))
返回字符串。空;
string contentType=string.Empty;
交换机(文件扩展名){
案例“htm”:
案例“html”:
contentType=“text/HTML”;
打破
案例“txt”:
contentType=“text/plain”;
打破
案例“doc”:
案例“rtf”:
案例“docx”:
contentType=“应用程序/msword”;
打破
案例“xls”:
案例“xlsx”:
contentType=“应用程序/x-msexcel”;
打破
案例“jpg”:
案例“jpeg”:
contentType=“image/jpeg”;
打破
案例“png”:
contentType=“image/png”;
打破
案例“bmp”:
contentType=“image/bmp”;
打破
案例“gif”:
contentType=“image/GIF”;
打破
案例“pdf”:
contentType=“application/pdf”;
打破
}
返回contentType;
}
公共无效登录(){
if(验证器==null)
authenticator=CreateAuthenticator();
if(driveService==null){
driveService=新的driveService(验证器);
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e){
string[]stringParts=FileUpload1.FileName.Split(新字符[]{'/'});
string str=stringParts[stringParts.Length-1].ToString();
字符串[]strType=str.Split(新字符[]{.'});
字符串类型=GetContentType(strType[1]);
Google.api.Drive.v2.Data.File newFile=new Google.api.Drive.v2.Data.File{Title=FileUpload1.FileName,MimeType=type};
byte[]byteArray=FileUpload1.FileBytes;
MemoryStream stream=新的MemoryStream(byteArray);
fileResource.InsertMediaUpload请求=driveService.Files.Insert(newFile,stream,newFile.MimeType);
request.Convert=true;
request.Upload();
Google.api.Drive.v2.Data.File File=request.ResponseBody;
InsertPermission(驱动服务,文件);
HttpContext.Current.Session[“GoogleAuthState”]=null;
Response.Write(file.AlternateLink);
}
public void InsertPermission(DriveService服务,Google.api.Drive.v2.Data.File){
权限newPermission=新权限();
newPermission.Value=TextBox1.Text;
newPermission.Type=“用户”;
newPermission.Role=“reader”;
试一试{
Permission per=service.Permissions.Insert(newPermission,file.Id).Fetch();
}
捕获(例外e){
Console.WriteLine(“发生错误:+e.Message”);
}
}