2017-07-18 2 views
0

Je suis un étudiant, où l'affectation de projet utilisait C# pour implémenter google drive sur le site Web. Maintenant, je le tester dans mon Visual Studio 2015. Lorsque je démarre le site à partir de mon VS, il va lancer un lecteur google authentifier. Mon authentification est réussie, je peux télécharger un fichier sur Drive mais l'onglet d'authentification ne se ferme pas. Voici mon code.L'onglet Authentification Google Drive n'est pas fermé

Default.aspx.cs

using System; 
using System.IO; 
using System.Web.UI; 

public partial class _Default : Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    var service = GoogleDriveHelper.AuthenticateOauth(); 
    } 
} 

GoogleDriveHelper.cs

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3; 
using Google.Apis.Services; 
using Google.Apis.Util.Store; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Threading; 

public class GoogleDriveHelper 
{ 
    public static DriveService AuthenticateOauth() 
    { 
    string[] scopes = new string[] { DriveService.Scope.Drive, 
            DriveService.Scope.DriveFile }; 

    try 
    { 
     string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
       credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); 

     UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets {       
             ClientId = "My-client-id",                      
             ClientSecret = "My-client-secret"    
            },          
            scopes,         
            "Users",                  
            CancellationToken.None,     
            new FileDataStore(credPath, true)).Result; 

     DriveService service = new DriveService(new BaseClientService.Initializer() { 
      HttpClientInitializer = credential, 
      ApplicationName = "Google Drive API Demo", 
     }); 

     return service; 
    } 
    catch 
    { 
     return null; 
    } 
} 

Ceci est mon onglet auth après authentifier Google Drive. Le problème est, il n'a pas fermé après l'authentification.

http://127.0.0.1:54972/authorize/?code=4/mbNAx__wjbnjsH6xJFtsgp3V6bDoweKrI8psJPaZd5E# 

Non stop loading auth tab

Voici donc la question que j'essaie de résoudre:

  1. Auto près auth onglet Google après l'authentification.
  2. Vous avez une autre erreur lors du déploiement de ce site dans IIS, l'authentification a échoué.

    Erreur: redirect_uri_mismatch

gars Merci à l'avance.

Répondre

0

Vous pouvez résoudre ce problème en accédant à l'API Google Developer Console et en spécifiant dans votre ID de client OAuth l'URI dans lequel vous utiliserez l'ID client.

sous autorisés Indiquez origines Javascript l'URI du site (www.mywebsiteexample.com) et sous redirect autorisés URIs l'URI avec un /oauth2callback.

enter image description here

+0

Ai-je besoin de créer ce oauth2callback? Ou c'est de google lui-même? – saf21