1

我正在尝试使用以下代码通过 CloudRail 访问 Google Drive。

// Actual string value removed.
private const string GDRIVE_CLIENT_ID = "client_id";
private const string ANDROID_PACKAGE_NAME = "package_name";
private const string CLOUDRAIL_APP_KEY = "app_key";
private readonly string REDIRECT_URL = $"{ANDROID_PACKAGE_NAME}:/oauth2redirect";

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    // Set CloudRail application key.
    CloudRail.AppKey = CLOUDRAIL_APP_KEY;

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    try
    {
        var googleDrive = new GoogleDrive(this, GDRIVE_CLIENT_ID, "", REDIRECT_URL, "state");
        googleDrive.UseAdvancedAuthentication();
        m_Service = googleDrive;

        Action act = () =>
        {
            var list = m_Service.GetChildren(@"/");
            // The function call never return.
        };
        var thread = new Thread(new ThreadStart(act));
        thread.Start();
    }
    catch (Exception ex)
    {
    }
}

调用后ICloudStorage.GetChildren(),我的应用程序被重定向到登录 Google 帐户。用户登录 Google 帐户并同意应用程序后,函数调用永远不会返回。也没有发现异常。

可能出了什么问题?

4

1 回答 1

1

我得到了 CloudRail 支持团队的回复,并在他们的帮助下设法解决了这个问题。

您需要在您的 活动之上包括IntentFilter和。您还需要输入方法,如下所示:LaunchModeSingleTaskOnNewIntent

[Activity(Label = "Awesome.Android", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }, DataScheme = "Android_Package_Name")]
public class MainActivity : Activity
{
    protected override void OnNewIntent(Intent intent)
    {
        CloudRail.AuthenticationResponse = intent;
        base.OnNewIntent(Intent);
    }
}

关键是相应的Activity类(在我的例子中,MainActivity)必须用IntentFilter. 此外,OnNewItent必须被覆盖并将响应传递回CloudRail.AuthenticationResponse覆盖。

我还发现android包名必须全小写,否则也不起作用。

编辑 [2018.07.19]

Android 包名必须至少包含一个句点字符 (.),否则可能会遇到invalid_request - missing authority错误。

于 2018-07-17T07:59:11.023 回答