You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android项目中使用WeDeploy API及OAuth跳转时获取Auth服务AccessToken

Hey there! Let's walk through your two WeDeploy questions for Android development clearly and practically:

1. Using the WeDeploy API in an Android Project

Getting started with the WeDeploy API in your Android app is straightforward—here's the step-by-step breakdown:

  • Add the necessary dependencies
    Open your app-level build.gradle file and include the WeDeploy Core dependency:

    dependencies {
        implementation 'com.wedeploy:wedeploy-core:2.0.0'
    }
    

    Sync your project to make sure the dependency installs correctly.

  • Initialize WeDeploy
    It’s best to initialize WeDeploy once when your app starts. You can do this in a custom Application class (or your main Activity if you don’t have one):

    import com.wedeploy.android.WeDeploy;
    
    public class MyApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            // Replace with your actual WeDeploy project URL
            WeDeploy.initialize("your-project-url.wedeploy.io");
        }
    }
    

    If you use a custom Application class, don’t forget to declare it in your AndroidManifest.xml.

  • Make API calls
    Once initialized, you can interact with your WeDeploy services. For example, fetching data from a database service:

    WeDeploy.data("your-service-name")
        .get("/items")
        .execute(new Callback<JsonObject>() {
            @Override
            public void onSuccess(JsonObject response) {
                // Handle the retrieved data here
            }
    
            @Override
            public void onFailure(Exception e) {
                // Handle any errors that occur
            }
        });
    }
    
2. Retrieving Access Token for WeDeploy Auth with OAuth Redirect

When using WeDeploy Auth’s OAuth flow that redirects back to an Activity, here’s how you can get the access token:

  • Configure your callback Activity
    First, set up your target Activity to receive the OAuth redirect in AndroidManifest.xml. Add an intent-filter to capture your redirect URL (replace placeholders with your actual values):

    <activity android:name=".OAuthCallbackActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Match your WeDeploy Auth redirect URI -->
            <data android:scheme="your-scheme" android:host="your-host" />
        </intent-filter>
    </activity>
    
  • Handle the redirect intent
    In your OAuthCallbackActivity, extract the authorization code from the incoming intent, then exchange it for an access token using the WeDeploy Auth SDK:

    import com.wedeploy.android.auth.WeDeployAuth;
    import com.wedeploy.android.auth.AuthException;
    
    public class OAuthCallbackActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            handleOAuthRedirect(getIntent());
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            handleOAuthRedirect(intent);
        }
    
        private void handleOAuthRedirect(Intent intent) {
            Uri uri = intent.getData();
            if (uri != null) {
                String code = uri.getQueryParameter("code");
                if (code != null) {
                    // Initialize Auth with your service URL
                    WeDeployAuth auth = WeDeployAuth.init("your-auth-service-url.wedeploy.io");
                    // Exchange code for access token (match redirect URL to your WeDeploy settings)
                    auth.exchange(code, "your-redirect-url")
                        .execute(new Callback<Session>() {
                            @Override
                            public void onSuccess(Session session) {
                                // Access token is available here
                                String accessToken = session.getAccessToken();
                                // Use this token for authenticated API requests
                            }
    
                            @Override
                            public void onFailure(Exception e) {
                                // Handle token exchange errors
                                if (e instanceof AuthException) {
                                    AuthException authException = (AuthException) e;
                                    // Handle specific authentication errors
                                }
                            }
                        });
                }
            }
        }
    }
    

    The Session object also provides refresh tokens and user details if you need them later. Just ensure the redirect URL used in exchange() matches exactly what’s configured in your WeDeploy Auth service.

内容的提问来源于stack exchange,提问作者albert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:44:23